【Flutter】 webview_flutter避坑

webview_flutter

webview_flutter没有SSL Error接口,也就是说等你的网页出现SSL 错误的时候这个插件无法捕捉处理,除非你改它的源码。

下面这段是webview_flutter官网的例子,它有onHttpError、onWebResourceError、但没有任何捕捉 SSL 错误的选项,我曾经不信邪找了很久。

controller = WebViewController()
  ..setJavaScriptMode(JavaScriptMode.unrestricted)
  ..setBackgroundColor(const Color(0x00000000))
  ..setNavigationDelegate(
    NavigationDelegate(
      onProgress: (int progress) {
        // Update loading bar.
      },
      onPageStarted: (String url) {},
      onPageFinished: (String url) {},
      onHttpError: (HttpResponseError error) {},
      onWebResourceError: (WebResourceError error) {},
      onNavigationRequest: (NavigationRequest request) {
        if (request.url.startsWith('https://www.youtube.com/')) {
          return NavigationDecision.prevent;
        }
        return NavigationDecision.navigate;
      },
    ),
  )
  ..loadRequest(Uri.parse('https://flutter.dev'));

通过日志发现可以取巧提醒SSL Error , SSL Error不会走onPageStarted,可以在这里加flag,然后在pageFinished的时候做判断即可。

flutter_inappwebview

但是另一个webview的插件flutter_inappwebview可以捕捉 ,这个插件感觉接口更多一点。

SSL Error接口使用onReceivedServerTrustAuthRequest捕捉。

                InAppWebView(
                      key: webViewKey,
                      initialUrlRequest: URLRequest(url: WebUri("https://www.baidu.com/?tn=02003390_20_hao_pg")),
                      initialSettings: settings,
                      pullToRefreshController: pullToRefreshController,
                      onWebViewCreated: (controller) {
                        webViewController = controller;
                      },
                    onReceivedServerTrustAuthRequest: (controller, challenge) async {
                      //解决 handshake failed问题
                      print("onReceivedServerTrustAuthRequest $challenge");
                      return ServerTrustAuthResponse(
                          action: ServerTrustAuthResponseAction.PROCEED);
                    },
                      onLoadStart: (controller, url) {
                        setState(() {
                          this.url = url.toString();
                          urlController.text = this.url;
                        });
                      },
                      onPermissionRequest: (controller, request) async {
                        return PermissionResponse(
                            resources: request.resources,
                            action: PermissionResponseAction.GRANT);
                      },
                        shouldOverrideUrlLoading:
                          (controller, navigationAction) async {
                        var uri = navigationAction.request.url!;
                        return NavigationActionPolicy.ALLOW;
                      },
                      onLoadStop: (controller, url) async {
                        pullToRefreshController?.endRefreshing();
                        setState(() {
                          this.url = url.toString();
                          urlController.text = this.url;
                        });
                      },
                      onReceivedError: (controller, request, error) {
                        pullToRefreshController?.endRefreshing();
                      },
                      onProgressChanged: (controller, progress) {
                        if (progress == 100) {
                          pullToRefreshController?.endRefreshing();
                        }
                        setState(() {
                          this.progress = progress / 100;
                          urlController.text = url;
                        });
                      },
                      onUpdateVisitedHistory: (controller, url, androidIsReload) {
                        setState(() {
                          this.url = url.toString();
                          urlController.text = this.url;
                        });
                      },
                      onConsoleMessage: (controller, consoleMessage) {
                        if (kDebugMode) {
                          print(consoleMessage);
                        }
                      },
                    ),

相关推荐

  1. Golang 指南

    2024-07-20 23:16:03       21 阅读
  2. PLIP,openbabel安装

    2024-07-20 23:16:03       50 阅读
  3. 使用VScode指南

    2024-07-20 23:16:03       42 阅读
  4. 深度学习指南

    2024-07-20 23:16:03       29 阅读
  5. Stable Diffusion指南

    2024-07-20 23:16:03       37 阅读
  6. 【Flutter】 webview_flutter

    2024-07-20 23:16:03       18 阅读
  7. 通过Redis增减库存

    2024-07-20 23:16:03       48 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-20 23:16:03       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-20 23:16:03       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-20 23:16:03       45 阅读
  4. Python语言-面向对象

    2024-07-20 23:16:03       55 阅读

热门阅读

  1. C++的模板(十二):forward模板

    2024-07-20 23:16:03       16 阅读
  2. Kotlin协程最佳实践

    2024-07-20 23:16:03       11 阅读
  3. SQL Server的魔法工坊:打造数据库的自定义函数

    2024-07-20 23:16:03       20 阅读
  4. Qt判定鼠标是否在该多边形的线条上

    2024-07-20 23:16:03       15 阅读
  5. 什么是虹膜识别技术

    2024-07-20 23:16:03       14 阅读
  6. C++/Qt 信号与槽

    2024-07-20 23:16:03       18 阅读
  7. CentOS Mysql8 数据库安装

    2024-07-20 23:16:03       16 阅读
  8. ubuntu22.04下YOLOv5 TensorRT模型部署

    2024-07-20 23:16:03       15 阅读