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

Flutter集成firebase_performance报错:Default FirebaseApp未初始化

解决Firebase Performance初始化顺序问题

问题原因

Firebase Performance的Android端初始化器会在应用启动阶段(早于Flutter Dart代码执行)自动触发,而你在main.dart中调用的Firebase.initializeApp是在Flutter层面执行的,顺序滞后,导致Performance组件尝试获取FirebaseApp时出现未初始化错误。

解决方案

1. 禁用Android端自动初始化

打开android/app/src/main/AndroidManifest.xml,在<application>标签内添加以下meta-data,禁用Performance的自动收集:

<meta-data
    android:name="firebase_performance_collection_enabled"
    android:value="false" />

2. Flutter端初始化完成后手动启用

在main.dart中,确保Firebase.initializeApp执行完成后,手动开启Performance收集:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
  // 手动启用Firebase Performance
  await FirebasePerformance.instance.setPerformanceCollectionEnabled(true);
  runApp(const MyApp());
}

3. 额外检查项

  • 确认firebase_core和firebase_performance依赖使用最新稳定版本,版本不兼容也可能引发此类问题
  • 验证google-services.json已正确放置在android/app目录下
  • 执行flutter clean清理缓存后重新构建项目

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 05:45:20