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

Firebase.initializeApp抛出类型错误:预期Object却得到Null

Flutter Firebase初始化抛出类型错误:Expected a value of type 'Object', but got one of type 'Null'

问题描述

调用Firebase.initializeApp时抛出类型错误,提示"Error: Expected a value of type 'Object', but got one of type 'Null'"。已尝试降级Flutter版本、firebase_core与firebase_auth依赖,更换Fedora系统,问题仍存在。非调试模式下因初始化失败,登录/登出操作会导致应用冻结,flutter doctor未检测到异常。

核心代码

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  );
  runApp(const MainApp());
}

异常详情

异常信息:
Error: Expected a value of type 'Object', but got one of type 'Null'
dart-sdk/lib/internal/js_dev_runtime/private/ddc_runtime/errors.dart 288:49 throw
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 121:3 castError
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 529:12 cast
dart-sdk/lib/_internal/js_dev_runtime/patch/core_patch.dart 78:24 as
packages/firebase_core_web/src/firebase_core_web.dart 87:20 get [_ignoredServiceScripts]
packages/firebase_core_web/src/firebase_core_web.dart 163:28 _initializeCore
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 84:54 runBody
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 123:5 _async
packages/firebase_core_web/src/firebase_core_web.dart 155:31 [_initializeCore]
packages/firebase_core_web/src/firebase_core_web.dart 197:11 initializeApp
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 84:54 runBody
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 123:5 _async
packages/firebase_core_web/src/firebase_core_web.dart 193:44 initializeApp
packages/firebase_core/src/firebase.dart 43:47 initializeApp
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 84:54 runBody
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 123:5 _async
packages/firebase_core/src/firebase.dart 39:43 initializeApp
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 84:54 runBody
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 123:5 _async
web_entrypoint.dart 24:31

dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 367:37 _checkAndCall
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 372:39 dcall
lib/ui/initialization.dart 77:15
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 45:50
dart-sdk/lib/async/zone.dart 1661:54 runUnary
dart-sdk/lib/async/future_impl.dart 147:18 handleValue
dart-sdk/lib/async/future_impl.dart 784:44 handleValueCallback
dart-sdk/lib/async/future_impl.dart 813:13 _propagateToListeners
dart-sdk/lib/async/future_impl.dart 584:5 [_completeWithValue]
dart-sdk/lib/async/future_impl.dart 657:7 callback
dart-sdk/lib/async/schedule_microtask.dart 40:11 _microtaskLoop
dart-sdk/lib/async/schedule_microtask.dart 49:5 _startMicrotaskLoop
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 177:15

解决方案

1. 检查DefaultFirebaseOptions配置

确认firebase_options.dart中的web平台配置参数(apiKey、authDomain、projectId等)是否完整、无空值。若配置为自动生成,重新运行flutterfire configure生成最新配置文件。

2. 清理缓存与构建产物

  • 执行flutter clean清理项目缓存
  • 删除web目录下的build文件夹
  • 重新执行flutter pub get后启动项目

3. 锁定依赖版本兼容性

确保firebase_core与firebase_core_web版本完全匹配,且适配当前Flutter版本。在pubspec.yaml中指定明确版本:

firebase_core: ^2.24.2
firebase_core_web: ^2.10.0

执行flutter pub upgrade更新依赖。

4. 手动传入web端配置

若自动生成的配置存在问题,直接手动传入Firebase web配置:

await Firebase.initializeApp(
  options: const FirebaseOptions(
    apiKey: "你的web apiKey",
    authDomain: "你的authDomain",
    projectId: "你的projectId",
    storageBucket: "你的storageBucket",
    messagingSenderId: "你的messagingSenderId",
    appId: "你的appId",
    measurementId: "你的measurementId", // 可选
  ),
);

5. 检查web/index.html脚本

确保web/index.html中无重复或版本冲突的Firebase脚本,若之前手动添加过脚本,建议移除,由Flutter插件自动管理。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 18:22:32