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

停电导致Flutter Web项目Firebase Options异常,求修复方法

问题描述

开发Flutter Web应用时,遭遇停电后Firebase配置异常,运行项目时出现以下错误:

Running "flutter pub get" in flutter_tools...
Launching lib\main.dart on Chrome in debug mode...
Waiting for connection from debug service on Chrome...
DartUri: Unresolved uri: dart:web_sql
DartUri: Unresolved uri: dart:ui
This app is linked to the debug service: ws://127.0.0.1:52488/knxO4BrQ_6M=/ws
Debug service listening on ws://127.0.0.1:52488/knxO4BrQ_6M=/ws
Running with sound null safety 
Debug service listening on ws://127.0.0.1:52488/knxO4BrQ_6M=/ws
Error: Assertion failed: 
file:///C:/Users/niema/AppData/Local/Pub/Cache/hosted/pub.dartlang.org/firebase_core_web-1.7.3/lib/src/firebase_core_web.dart:207:11
options != null
"FirebaseOptions cannot be null when creating the default app."
at Object.throw_ [as throw] (http://localhost:52440/dart_sdk.js:5069:11)
at Object.assertFailed (http://localhost:52440/dart_sdk.js:4994:15)
at firebase_core_web.FirebaseCoreWeb.new.initializeApp (http://localhost:52440/packages/firebase_core_web/firebase_core_web.dart.lib.js:226:42)
at initializeApp.next (<anonymous>)
at http://localhost:52440/dart_sdk.js:40627:33
at _RootZone.runUnary (http://localhost:52440/dart_sdk.js:40497:59)
at _FutureListener.thenAwait.handleValue (http://localhost:52440/dart_sdk.js:35424:29)
at handleValueCallback (http://localhost:52440/dart_sdk.js:35985:49)
at _Future._propagateToListeners (http://localhost:52440/dart_sdk.js:36023:17)
at [_completeWithValue] (http://localhost:52440/dart_sdk.js:35858:23)
at http://localhost:52440/dart_sdk.js:35063:46
at _RootZone.runUnary (http://localhost:52440/dart_sdk.js:40497:59)
at _FutureListener.then.handleValue (http://localhost:52440/dart_sdk.js:35424:29)
at handleValueCallback (http://localhost:52440/dart_sdk.js:35985:49)
at _Future._propagateToListeners (http://localhost:52440/dart_sdk.js:36023:17)
at [_completeWithValue] (http://localhost:52440/dart_sdk.js:35858:23)
at async._AsyncCallbackEntry.new.callback (http://localhost:52440/dart_sdk.js:35892:35)
at Object._microtaskLoop (http://localhost:52440/dart_sdk.js:40764:13)
at _startMicrotaskLoop (http://localhost:52440/dart_sdk.js:40770:13)
at http://localhost:52440/dart_sdk.js:36247:9

已尝试以下操作但未解决问题:

  • 删除pubspec.lock文件(因出现001001二进制错误)
  • 执行flutter clean和flutter pub get命令
  • 删除Flutter文件夹中的pub缓存
  • 删除C:/Users/niema/AppData/Local/Pub路径下的缓存文件夹
  • 从GitHub重新克隆项目
修复方案

1. 检查Firebase配置文件完整性

停电可能导致配置文件损坏,重点验证两处:

  • 查看lib/firebase_options.dart,确认DefaultFirebaseOptions包含完整的Web配置参数(apiKey、appId、projectId等),无语法错误或缺失。
  • 检查web/index.html,确认Firebase初始化脚本的配置参数完整,示例如下:
    <script type="module">
      import { initializeApp } from "https://www.gstatic.com/firebasejs/9.22.0/firebase-app.js";
      const firebaseConfig = {
        apiKey: "YOUR_API_KEY",
        authDomain: "YOUR_AUTH_DOMAIN",
        projectId: "YOUR_PROJECT_ID",
        storageBucket: "YOUR_STORAGE_BUCKET",
        messagingSenderId: "YOUR_SENDER_ID",
        appId: "YOUR_APP_ID"
      };
      const app = initializeApp(firebaseConfig);
    </script>
    

2. 重新生成Firebase配置文件

若配置文件损坏,直接从Firebase控制台重新获取:

  • 进入Firebase控制台对应项目,点击「项目设置」
  • 下滑到「您的应用」区域,找到Web应用并点击「配置」
  • 复制完整配置参数,更新到firebase_options.dart和web/index.html中

3. 验证Firebase初始化代码

确保main.dart中初始化代码正确传入配置参数:

import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform, // 确认此处未传入null
  );
  runApp(const MyApp());
}

检查DefaultFirebaseOptions.currentPlatform是否针对Web平台返回有效配置,而非null。

4. 清理Web构建缓存

删除项目根目录下的build/web文件夹,执行flutter build web --debug重新构建Web项目,再运行flutter run -d chrome。

5. 检查依赖版本兼容性

确认Dart SDK与Firebase依赖版本匹配:

  • 查看pubspec.yaml中Firebase相关依赖版本,比如firebase_core: ^2.15.0(根据Flutter版本调整)
  • 执行flutter pub upgrade更新所有依赖到兼容版本

内容的提问来源于stack exchange,提问作者Mario Niemand

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 01:20:32