Flutter - Errors when trying to intialize Firebase with default settings

530

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.

Share:
530
stackunderflow
Author by

stackunderflow

Updated on January 03, 2023

Comments

  • stackunderflow
    stackunderflow over 1 year

    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.

  • stackunderflow
    stackunderflow about 2 years
    ok tq for the answer. i'll try it
  • Peter Haddad
    Peter Haddad about 2 years
    @stackunderflow great, tell me how it goes
  • stackunderflow
    stackunderflow about 2 years
    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?
  • Peter Haddad
    Peter Haddad about 2 years
    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