Flutter Web集成firebase_core遇方法未找到错误的解决
Flutter Web集成Firebase Core后Chrome运行崩溃问题
问题描述
在Chrome上运行Flutter应用时出现崩溃,该问题仅在安装firebase_core后触发,Android和iOS平台运行正常。已按Firebase控制台步骤完成Web应用与现有Firebase Cloud的关联配置。
相关代码与配置
初始化代码
import "package:firebase_core/firebase_core.dart" as core; void main() async { WidgetsFlutterBinding.ensureInitialized(); await core.Firebase.initializeApp( options: core.FirebaseOptions( apiKey: "apiKey", appId: "appId", messagingSenderId: "messagingSenderId", projectId: " projectId" ) ); runApp(const MyApp()); }
HTML脚本配置
在index.html的<body>标签内添加了以下脚本:
<script type="module"> // Import the functions you need from the SDKs you need import { initializeApp } from "firebase/app"; import { getAnalytics } from "firebase/analytics"; // TODO: Add SDKs for Firebase products that you want to use // Your web app's Firebase configuration // For Firebase JS SDK v7.20.0 and later, measurementId is optional const firebaseConfig = { apiKey: "my apikey", authDomain: "my authDomain", databaseURL: "databaseURL", projectId: "projectId", storageBucket: "storageBucket", messagingSenderId: "messagingSenderId", appId: "appId", measurementId: "measurementId" }; // Initialize Firebase const app = initializeApp(firebaseConfig); const analytics = getAnalytics(app); </script>
pubspec.yaml依赖配置
dependencies: flutter: sdk: flutter # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. cupertino_icons: ^1.0.2 firebase_core: ^2.1.1
错误信息
运行时抛出大量方法未找到的错误,示例如下:
../../../flutter/.pub-cache/hosted/pub.dartlang.org/firebase_core_web-2.0.1/lib/firebase_core_web.dart:46:15: Error: Method not found: 'getProperty'. if (js_util.getProperty(e, 'name') == 'FirebaseError') { ^^^^^^^^^^^ ...(省略其余错误信息)
已尝试的无效方案
- 查阅各类教程调整配置
- 手动添加
firebase_core_web依赖包 - 降级Firebase相关包版本
- 升级Flutter至最新稳定版
- 新建空白Flutter项目安装
firebase_core后问题复现
最终修复方案
执行以下命令修复pub缓存文件夹,解决了问题:
flutter pub cache repair
内容的提问来源于stack exchange,提问作者Timmophe
相关产品推荐
相关产品推荐

