Flutter Web报错pluginConstants['isCrashlyticsCollectionEnabled'] != null求助
问题背景
现有适配iOS和Android端的Flutter应用,需要迁移到Web端运行,启动时触发如下错误:
runZonedGuarded: Caught error in my root zone.
pluginConstants['isCrashlyticsCollectionEnabled'] != null is not true
此前收到的解决方案是添加kIsWeb平台判断即可解决,已经按要求添加判断逻辑但依旧触发相同报错,相关代码如下:
main.dart入口代码
void main() async { WidgetsFlutterBinding.ensureInitialized(); if(!kIsWeb) { await Firebase.initializeApp(); } runZonedGuarded(() async { if (kDebugMode) { await FirebaseCrashlytics.instance.setCrashlyticsCollectionEnabled(false); }else{ await FirebaseCrashlytics.instance.setCrashlyticsCollectionEnabled(true); } runApp(App( authenticationRepository: AuthenticationRepository(), userRepository: UserRepository(), )); }, (error, stackTrace) async { print('runZonedGuarded: Caught error in my root zone. $error'); }); }
web/index.html配置代码
<!DOCTYPE html> <html> <head> <!-- The core Firebase JS SDK is always required and must be listed first --> <script src="https://www.gstatic.com/firebasejs/8.6.1/firebase-app.js"></script> <script src="https://www.gstatic.com/firebasejs/8.6.1/firebase-auth.js"></script> <script src="https://www.gstatic.com/firebasejs/8.6.1/firebase-analytics.js"></script> <script src="https://www.gstatic.com/firebasejs/8.6.1/firebase-firestore.js"></script> <script src="https://www.gstatic.com/firebasejs/8.6.1/firebase-messaging.js"></script> <script src="https://www.gstatic.com/firebasejs/8.6.1/firebase-firestore.js"></script> <!-- TODO: Add SDKs for Firebase products that you want to use https://firebase.google.com/docs/web/setup#available-libraries --> <script> // Your web app's Firebase configuration var firebaseConfig = { apiKey: "xxx", authDomain: "xxx", databaseURL: "xxx", projectId: "xxxx", storageBucket: "xxx", messagingSenderId: "xxx", appId: "xxxx" }; // Initialize Firebase firebase.initializeApp(firebaseConfig); firebase.analytics(); </script> <!-- If you are serving your web app in a path other than the root, change the href value below to reflect the base path you are serving from. The path provided below has to start and end with a slash "/" in order for it to work correctly. Fore more details: * https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base --> <base href="/"> <meta charset="UTF-8"> <meta content="IE=Edge" http-equiv="X-UA-Compatible"> <meta name="description" content="A new Flutter project."> <!-- iOS meta tags & icons --> <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="apple-mobile-web-app-status-bar-style" content="black"> <meta name="apple-mobile-web-app-title" content="cashbac_biz"> <link rel="apple-touch-icon" href="icons/Icon-192.png"> <!-- Favicon --> <link rel="icon" type="image/png" href="favicon.png"/> <title>cashbac_biz</title> <link rel="manifest" href="manifest.json"> </head> <body> <!-- This script installs service_worker.js to provide PWA functionality to application. For more information, see: https://developers.google.com/web/fundamentals/primers/service-workers --> <script> if ('serviceWorker' in navigator) { window.addEventListener('flutter-first-frame', function () { navigator.serviceWorker.register('flutter_service_worker.js'); }); } </script> <script src="main.dart.js" type="application/javascript"></script> </body> </html>
修复方案
你只在Firebase初始化逻辑上加了平台判断,Crashlytics的调用逻辑没有做Web端兼容,Firebase Crashlytics暂不支持Flutter Web平台,需要给所有Crashlytics相关代码都加上非Web平台的判断:
void main() async { WidgetsFlutterBinding.ensureInitialized(); if(!kIsWeb) { await Firebase.initializeApp(); } runZonedGuarded(() async { // 只在非Web端调用Crashlytics相关逻辑 if(!kIsWeb) { if (kDebugMode) { await FirebaseCrashlytics.instance.setCrashlyticsCollectionEnabled(false); }else{ await FirebaseCrashlytics.instance.setCrashlyticsCollectionEnabled(true); } } runApp(App( authenticationRepository: AuthenticationRepository(), userRepository: UserRepository(), )); }, (error, stackTrace) async { print('runZonedGuarded: Caught error in my root zone. $error'); // 非Web端才上报异常到Crashlytics if(!kIsWeb) { await FirebaseCrashlytics.instance.recordError(error, stackTrace); } }); }
如果需要Web端的异常上报能力,可以单独接入Web端兼容的日志上报服务,无需依赖Crashlytics。
内容的提问来源于stack exchange,提问作者Rohmatul Laily
相关产品推荐
相关产品推荐

