You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS端WebViewWidget类型转换错误:NSNull转AuthenticationChallengeResponse失败

Flutter webview_flutter iOS端类型转换错误:NSNull转AuthenticationChallengeResponse失败

问题详情

使用webview_flutter包开发支付WebView时,iOS端出现崩溃错误:

Could not cast value of type NSNull (0x1ec725448) to 'webview_flutter_wkwebview.AuthenticationChallengeResponse' (0x10591eef8)

触发场景

  • WebView加载支付页面时触发
  • 目标URL在浏览器中可正常访问,但WebView与页面交互时抛出错误
  • 已配置NavigationDelegate处理页面加载、JavaScript及错误逻辑

复现代码

import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';

class PaymentWebview extends StatefulWidget {
  final String initialUrl;

  const PaymentWebview({super.key, required this.initialUrl});

  @override
  State<PaymentWebview> createState() => _PaymentWebviewState();
}

class _PaymentWebviewState extends State<PaymentWebview> {
  late final WebViewController _controller;

  @override
  void initState() {
    super.initState();

    // WebView初始化
    try {
      late final PlatformWebViewControllerCreationParams params;

      if (WebViewPlatform.instance is WebKitWebViewPlatform) {
        params = WebKitWebViewControllerCreationParams(
          allowsInlineMediaPlayback: true,
          mediaTypesRequiringUserAction: const <PlaybackMediaTypes>{},
        );
      } else {
        params = const PlatformWebViewControllerCreationParams();
      }

      final WebViewController controller =
          WebViewController.fromPlatformCreationParams(params);

      controller
        ..setJavaScriptMode(JavaScriptMode.unrestricted)
        ..setNavigationDelegate(
          NavigationDelegate(
            onProgress: (int progress) {
              debugPrint('加载进度: $progress%');
            },
            onPageStarted: (String url) {
              debugPrint('页面开始加载: $url');
            },
            onPageFinished: (String url) {
              if (!mounted) return; // 检查页面是否已销毁

              if (url.contains("success")) {
                print("✅ 支付成功: $url");
                if (mounted) {
                  Navigator.pop(context, true);
                }
              } else if (url.contains("fail")) {
                print("❌ 支付失败: $url");
                if (mounted) Navigator.pop(context, false);
              }
            },
            onHttpError: (HttpResponseError error) {
              debugPrint('HttpResponseError: ${error.response}');
            },
            onWebResourceError: (WebResourceError error) {
              debugPrint('WebResourceError: ${error.description}');
            },
            onHttpAuthRequest: null,
            onNavigationRequest: (NavigationRequest request) {
              return NavigationDecision.navigate;
            },
          ),
        )
        ..loadRequest(Uri.parse(widget.initialUrl));

      _controller = controller;
    } catch (ex) {
      print('WebView初始化错误: $ex');
    }
  }

  @override
  void dispose() {
    if (mounted) {
      _controller.clearCache();
    }
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text("Payment")),
      body: Stack(
        children: [
          WebViewWidget(controller: _controller),
        ],
      ),
    );
  }
}

解决方案

1. 修复onHttpAuthRequest空回调问题

错误根源是iOS端WKWebView处理身份验证挑战时,onHttpAuthRequest设为null会导致原生层类型转换失败。需替换为具体的默认处理逻辑:

onHttpAuthRequest: (HttpAuthRequest request) async {
  // 返回默认处理行为,避免空值引发的类型转换异常
  return HttpAuthResponse(action: HttpAuthAction.proceed);
},

2. 升级webview_flutter依赖版本

该问题在webview_flutter的部分旧版本中存在,建议升级到最新稳定版:
修改pubspec.yaml中的依赖:

dependencies:
  webview_flutter: ^4.4.0 # 替换为当前最新稳定版本

执行flutter pub get更新依赖后重新编译。

3. 额外排查建议

  • 检查支付页面是否包含需要HTTP身份验证的资源,若有需在onHttpAuthRequest中针对性处理(如提供账号密码)
  • 清理项目缓存:执行flutter clean后重新运行iOS项目

错误原因说明

此错误是iOS平台webview_flutter_wkwebview插件的底层问题:当onHttpAuthRequest未提供回调时,原生代码收到NSNull值却尝试强制转换为AuthenticationChallengeResponse类型,从而触发类型转换崩溃。

内容的提问来源于stack exchange,提问作者Cavid Haciyev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 16:43:16