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

Flutter WebView抛出PlatformException:InstanceManager无对应NSURL实例

问题:webview_flutter 4.2.2 iOS 环境下跳转特定链接触发 PlatformException 异常

实现代码

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

  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);
  // #enddocregion platform_features

  controller
    ..setJavaScriptMode(JavaScriptMode.unrestricted)
    ..setBackgroundColor(const Color(0x00000000))
    ..setNavigationDelegate(
      NavigationDelegate(
        onProgress: (int progress) {
          debugPrint('WebView is loading (progress : $progress%)');
        },
        onPageStarted: (String url) {
          debugPrint('Page started loading: $url');
        },
        onPageFinished: (String url) {
          debugPrint('Page finished loading: $url');
        },
        onWebResourceError: (WebResourceError error) {
          debugPrint('''
Page resource error:
code: ${error.errorCode}
description: ${error.description}
errorType: ${error.errorType}
isForMainFrame: ${error.isForMainFrame}
        ''');
        },
        onNavigationRequest: (NavigationRequest request) {
          if (request.url
              .startsWith('https://test.example.com/subscriptions/intent')) {
            var provider = context.read<StoreInfoProvider>();
            provider.setPaymentUrl(true);
            return NavigationDecision.prevent;
          }
          return NavigationDecision.navigate;
        },
        onUrlChange: (UrlChange change) {
          debugPrint('url change to ${change.url}');
        },
      ),
    )
    ..addJavaScriptChannel(
      'Toaster',
      onMessageReceived: (JavaScriptMessage message) {
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(content: Text(message.message)),
        );
      },
    )
    ..loadRequest(Uri.parse(widget.result == PaymentResults.success
        ? Strings.profileUrl
        : widget.result == PaymentResults.fail
            ? Strings.failedUrl
            : Strings.mainUrl));
  _controller = controller;
}

异常信息

[VERBOSE-2:dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(NSInternalInconsistencyException, InstanceManager does not contain an NSURL with identifier: 65540, null, null)
#0 NSUrlHostApi.getAbsoluteString (package:webview_flutter_wkwebview/src/common/web_kit.g.dart:2799:7)
<asynchronous suspension>
#1 WebKitWebViewController._webView.<anonymous closure>.<anonymous closure> (package:webview_flutter_wkwebview/src/webkit_webview_controller.dart:242:48)
<asynchronous suspension>

问题原因

这个异常是 webview_flutter_wkwebview 插件(iOS平台WebView的底层实现)的内部逻辑问题导致的:

  • 当你在 onNavigationRequest 回调中返回 NavigationDecision.prevent 阻止目标URL跳转时,WKWebView底层会提前释放对应的NSURL实例
  • 但插件后续的回调逻辑仍尝试通过InstanceManager(用于管理Dart与原生对象的映射关系)获取这个已经被释放的NSURL实例,从而触发"InstanceManager does not contain an NSURL with identifier"错误

内容的提问来源于stack exchange,提问作者Amir Ali Aghamali

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 13:42:43