Previously I getting this No Firebase App '[DEFAULT]' has been created error when trying to copy the code from my previous Flutter project, which required me to import firebase_core
and run Firebase.initializeApp()
before runApp()
.
Unfortunately I getting below errors when trying to properly follow the installation instruction here https://pub.dev/packages/firebase_core, caused by unresolvable references on these two lines
import 'package:firebase_core_example/firebase_config.dart'
FirebaseApp app = await Firebase.initializeApp(options : DefaultFirebaseConfig.platformOptions)
So I wonder where that firebase_core_example
and it's DefaultFirebaseConfig
class actually came from because I can't find anywhere from pub.dev. Surely the default settings should be allowed because all the required informations has already been included in android/app/google-services.json
file. I don't think I need to hardcode it again in Dart file.
CodePudding user response:
The DefaultFirebaseConfig
is a custom class, it contains the FirebaseOptions
for each platform:
import 'dart:io';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/foundation.dart';
class DefaultFirebaseConfig {
static FirebaseOptions get platformOptions {
if (kIsWeb) {
// Web
return const FirebaseOptions(
appId: '1:448618578101:web:0b650370bb29e29cac3efc',
apiKey: 'AIzaSyAgUhHU8wSJgO5MVNy95tMT07NEjzMOfz0',
projectId: 'react-native-firebase-testing',
messagingSenderId: '448618578101',
);
} else if (Platform.isIOS || Platform.isMacOS) {
// iOS and MacOS
return const FirebaseOptions(
appId: '1:448618578101:ios:0b650370bb29e29cac3efc',
apiKey: 'AIzaSyAgUhHU8wSJgO5MVNy95tMT07NEjzMOfz0',
projectId: 'react-native-firebase-testing',
messagingSenderId: '448618578101',
iosBundleId: 'io.flutter.plugins.firebasecoreexample',
);
} else {
// Android
return const FirebaseOptions(
appId: '1:448618578101:android:0446912d5f1476b6ac3efc',
apiKey: 'AIzaSyCuu4tbv9CwwTudNOweMNstzZHIDBhgJxA',
projectId: 'react-native-firebase-testing',
messagingSenderId: '448618578101',
);
}
}
}
If you are only targeting Android platform and have added the google-services.json
file then you can just do:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
without providing the FirebaseOptions
argument.
CodePudding user response:
Peter Haddad's answer should have worked but unfortunately in my case it's produced this runtime error which I still have no clue why.
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
. View the documentation for more information: https://firebase.flutter.dev/docs/overview#initialization
So I just followed the link and proceeded with the installation of CLIs of Firebase and FlutterFire. flutterfire configure
generated firebase_options.dart
file with content similar to above answer except with the class.method
name changed to DefaultFirebaseOptions.currentPlatform