![](/img/trans.png)
[英]Flutter Firebase has not been correctly initialized. Have you added the "google-services.json" file to the project?
[英]Flutter: Firebase has not been correctly initialized
我正在开发 iPhone 12 Pro Max 模拟器,macOS Catalina。
当我尝试运行应用程序时出现此错误:
[VERBOSE-2:ui_dart_state.cc(209)] Unhandled Exception: [core/not-initialized] Firebase has not been correctly initialized.
控制台中还有一个提示:通常这意味着您在调用Firebase.initializeApp
之前尝试使用 Firebase 服务。
我在使用之前初始化了 Firebase。 像这样:
void main() async {
print('-- main');
WidgetsFlutterBinding.ensureInitialized();
print('-- WidgetsFlutterBinding.ensureInitialized');
await Firebase.initializeApp();
print('-- main: Firebase.initializeApp');
runApp(const MyApp());
}
这是我在控制台 output 中看到的:
Xcode build done. 132.9s
flutter: -- main
flutter: -- WidgetsFlutterBinding.ensureInitialized
[VERBOSE-2:ui_dart_state.cc(209)] Unhandled Exception: [core/not-initialized] Firebase has not been correctly initialized.
Usually this means you've attempted to use a Firebase service before calling `Firebase.initializeApp`.
我在控制台中看不到-- main: Firebase.initializeApp
行。 所以它在第一次尝试初始化 Firebase 时失败了。
我在 Firebase 中创建 Android/Apple 应用程序。 下载google-services.json
/ GoogleService-Info.plist
并放入项目中。
我没有使用 android,但我在 build.gradle: classpath 'com.google.gms:google-services:4.3.10'
添加了依赖项
和 app/build.gradle: apply plugin: 'com.google.gms.google-services'
依赖项:
firebase_auth: ^3.3.5
firebase_messaging: ^10.0.9
google_sign_in: ^5.2.1
flutter --版本:
Flutter 2.5.3 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 18116933e7 (3 months ago) • 2021-10-15 10:46:35 -0700
Engine • revision d3ea636dc5
Tools • Dart 2.14.4
我怎么解决这个问题? 顺便说一句,我正在开发一个全新的 flutter 项目。
这是我修复此错误的方法:
您可以使用以下命令简单地添加它:
flutter pub add firebase_core
将 firebase 插件添加到您的主文件中:
import 'package:firebase_core/firebase_core.dart'; import 'firebase_options.dart';
将您的 void main function 替换为异步的:
void main() async { WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp( options: DefaultFirebaseOptions.currentPlatform, ); runApp(const YourAppGoesHere()); }
当我在 Xcode 中添加 GoogleService-Info.plist 文件时,我使用了错误的名称 GoogleService-Info**(1)**.plist。 如果您在下载中有相同的文件,mac 会在下一个下载的文件中添加多个副本。
当您将google-services.json
添加到 iOS 项目时,您需要使用 Xcode 添加它,如以下文档中所述:
https://firebase.flutter.dev/docs/manual-installation/ios
如果您通读该页面,您会发现以下注释:
通过文件系统手动添加 [google-service.json] 不会将文件链接到项目
您需要尝试然后重新启动您的应用程序(重建它)。
编辑:附加说明:
您还需要将pubspec.yaml
firebase_core
的依赖项中。
你得到这个答案了吗? 自从我尝试添加苹果登录后,我遇到了同样的问题。
您必须在 Xcode 中添加GoogleService-Info.plist
,而不是将其放入文件夹中:
所以它看起来像这样:
来源: https://www.kindacode.com/article/flutter-correctly-adding-googleservices-info-plist-to-ios/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.