3

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.

0

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

4 Comments

ok tq for the answer. i'll try it
@stackunderflow great, tell me how it goes
call initializeApp without parameters doesn't work for me so i used flutterfire to generate the default config file as per instructions. have you any idea why?
Did you use await, the flutterfire cli will generate it for you and it's the new way. But the manual way is just two steps literally.. In the cli way you don't have to put the Google-services.json since you specify the options while in the manual way you do have to do add it. But if it works cli then just do that
1

Peter Haddad's answer should work but unfortunately in my case it's produced this runtime error which I 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.getter name changed to DefaultFirebaseOptions.currentPlatform, so I still have to initialize Firebase with options parameter, Firebase.initializeApp(options : DefaultFirebaseOptions.currentPlatform)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.