简体   繁体   中英

Flutter: Firebase has not been correctly initialized

I'm working on iPhone 12 Pro Max Emulator, macOS Catalina.

I'm getting this error when I try to run the app:

[VERBOSE-2:ui_dart_state.cc(209)] Unhandled Exception: [core/not-initialized] Firebase has not been correctly initialized.

Also there is a tip in the console: Usually this means you've attempted to use a Firebase service before calling Firebase.initializeApp .

I initialize the Firebase before use it. Like this:

void main() async {
  print('-- main');

  WidgetsFlutterBinding.ensureInitialized();
  print('-- WidgetsFlutterBinding.ensureInitialized');

  await Firebase.initializeApp();
  print('-- main: Firebase.initializeApp');

  runApp(const MyApp());
}

This is what I see in the console 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`.

I can't see the -- main: Firebase.initializeApp line in the console. So it fails in first trying to initialize the Firebase.

I create Android/Apple apps in Firebase. Downloaded google-services.json / GoogleService-Info.plist and put in the project.

  • GoogleService-Info.plist:

iOS

  • google-services.json:

安卓

I'm not using the android, but I added dependency into build.gradle: classpath 'com.google.gms:google-services:4.3.10'

And app/build.gradle: apply plugin: 'com.google.gms.google-services'

dependencies:

firebase_auth: ^3.3.5
firebase_messaging: ^10.0.9
google_sign_in: ^5.2.1

flutter --version:

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

How can I solve this problem? BTW, I'm working on a brand new flutter project.

Here's how I fixed this error:

  1. Ensure that all firebase services have been added to your pubspec.yaml file, in the dependencies section. firebase_core appears to be missing and is required to connect your flutter app to your firebase project.

You can simply add it using this command:

   flutter pub add firebase_core 
  1. Add the firebase plugins to your main file:

     import 'package:firebase_core/firebase_core.dart'; import 'firebase_options.dart';
  2. Replace your void main function with an asynchronous one:

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

When I add the GoogleService-Info.plist file in Xcode, I used the wrong name GoogleService-Info**(1)**.plist. If you have the same file in downloads, mac adds a number of copy to the next downloaded file.

When you add google-services.json to an iOS project, you need to add it using Xcode as described in the following document:

https://firebase.flutter.dev/docs/manual-installation/ios

If you read through the page, you'll find the following note:

adding [google-service.json] manually via the filesystem won't link the file to the project

You need to try that then restart your app (rebuild it).

Edit: Additional Note:

You'll also need to add firebase_core to your dependencies in pubspec.yaml .

Did you get an answer to this? I am getting the same problem ever since i tried to also add apple sign in.

You have to add the GoogleService-Info.plist in Xcode instead of just placing it into the folder:

单击 Runner > 将文件添加到“Runner”

So it looks like this:

GoogleServices-Info.plist 直接在“Runner”项目中

Source: https://www.kindacode.com/article/flutter-correctly-adding-googleservices-info-plist-to-ios/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM