Flutter iOS页面切换触发NSInvalidArgumentException异常求助
解决Flutter iOS页面切换时的NSInvalidArgumentException崩溃
崩溃日志
FlutterWebViewController - dealloc LeakAvoider - dealloc InAppWebViewMethodHandler - dealloc WebMessageListener - dealloc 2023-08-02 13:19:24.452817+0900 Runner[29165:2689182] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil' *** First throw call stack: (0x1cb5a5a24 0x1c4995958 0x1cb733524 0x1cb730358 0x1cb5af974 0x105f9eb4c 0x106348f44 0x1063499f4 0x10624e878 0x1063488a0 0x10634a970 0x106349c40 0x10634a628 0x10624ceb0 0x106250cac 0x1cb660c6c 0x1cb620bfc 0x1cb5c83f0 0x1cb613408 0x1cb617b48 0x201993984 0x1cd9c2638 0x1cd9c22b0 0x10247af64 0x1e83a1df0) libc++abi: terminating with uncaught exception of type NSException
排查与修复方案
升级in_app_webview插件:该崩溃大概率是插件旧版本的iOS端资源释放bug导致,执行命令升级到最新稳定版:
flutter pub upgrade in_app_webview页面销毁时主动清理WebView资源:在包含WebView的StatefulWidget的
dispose方法中,手动清理WebView实例,避免内存释放时出现空对象操作:@override void dispose() { // 先加载空白页释放资源 _webViewController?.loadUrl(urlRequest: URLRequest(url: Uri.parse('about:blank'))); // 销毁WebViewController _webViewController?.dispose(); super.dispose(); }移除所有WebMessageListener:崩溃日志显示WebMessageListener在释放,可能是注册的listener未正确移除,在dispose中添加移除逻辑:
@override void dispose() { _webViewController?.removeAllWebMessageListeners(); _webViewController?.dispose(); super.dispose(); }检查页面切换的生命周期时序:如果使用Navigator进行页面切换,确保在页面pop前完成WebView的资源清理,可通过await等待异步操作完成:
// 比如在返回按钮点击时 onPressed: () async { await _webViewController?.dispose(); Navigator.pop(context); },逐步禁用特性排查:如果以上方法无效,尝试禁用WebView的JavaScript、WebMessage通道等特性,逐步定位具体触发崩溃的功能,再针对性处理。
内容的提问来源于stack exchange,提问作者김첨지
相关产品推荐
相关产品推荐

