![](/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.