initializeApp method

  1. @override
Future<FirebaseAppPlatform> initializeApp({
  1. String? name,
  2. FirebaseOptions? options,
})
override

Initializes a new FirebaseAppPlatform instance by name and options and returns the created app. This method should be called before any usage of FlutterFire plugins.

The default app instance cannot be initialized here and should be created using the platform Firebase integration.

Implementation

@override
Future<FirebaseAppPlatform> initializeApp({
  String? name,
  FirebaseOptions? options,
}) async {
  if (!_isRequireJsDefined) {
    await _initializeCore();
  } else {
    await _initializeCoreRequireJs();
  }

  try {
    firebase.SDK_VERSION;
  } catch (e) {
    if (e
        .toString()
        .contains("Cannot read property 'SDK_VERSION' of undefined")) {
      throw coreNotInitialized();
    }
  }

  assert(
    () {
      if (firebase.SDK_VERSION != supportedFirebaseJsSdkVersion) {
        // ignore: avoid_print
        print(
          '''
          WARNING: FlutterFire for Web is explicitly tested against Firebase JS SDK version "$supportedFirebaseJsSdkVersion"
          but your currently specifying "${firebase.SDK_VERSION}" by either the imported Firebase JS SDKs in your web/index.html
          file or by providing an override - this may lead to unexpected issues in your application. It is recommended that you change all of the versions of the
          Firebase JS SDK version "$supportedFirebaseJsSdkVersion":

          If you override the version manually:
            change:
              <script>window.flutterfire_web_sdk_version = '${firebase.SDK_VERSION}';</script>
            to:
              <script>window.flutterfire_web_sdk_version = '$supportedFirebaseJsSdkVersion';</script>

          If you import the Firebase scripts in index.html, instead allow FlutterFire to manage this for you by removing
          any Firebase scripts in your web/index.html file:
              e.g. remove: <script src="https://www.gstatic.com/firebasejs/${firebase.SDK_VERSION}/firebase-app.js"></script>
        ''',
        );
      }

      return true;
    }(),
  );

  firebase.App? app;

  if (name == null || name == defaultFirebaseAppName) {
    bool defaultAppExists = false;

    try {
      app = firebase.app();
      defaultAppExists = true;
    } catch (e) {
      // noop
    }

    if (defaultAppExists) {
      if (options != null) {
        // If there is a default app already and the user provided options do a soft
        // check to see if options are roughly identical (so we don't unnecessarily
        // throw on minor differences such as platform specific keys missing,
        // e.g. hot reloads/restarts).
        if (options.apiKey != app!.options.apiKey ||
            options.databaseURL != app.options.databaseURL ||
            options.storageBucket != app.options.storageBucket) {
          // Options are different; throw.
          throw duplicateApp(defaultFirebaseAppName);
        }
      }
    } else {
      assert(
        options != null,
        'FirebaseOptions cannot be null when creating the default app.',
      );

      // At this point, there is no default app so we need to create it with
      // the users options.
      app = firebase.initializeApp(
        apiKey: options!.apiKey,
        authDomain: options.authDomain,
        databaseURL: options.databaseURL,
        projectId: options.projectId,
        storageBucket: options.storageBucket,
        messagingSenderId: options.messagingSenderId,
        appId: options.appId,
        measurementId: options.measurementId,
      );
    }
  }

  // Ensure the user has provided options for secondary apps.
  if (name != null && name != defaultFirebaseAppName) {
    assert(
      options != null,
      'FirebaseOptions cannot be null when creating a secondary Firebase app.',
    );

    try {
      app = firebase.initializeApp(
        name: name,
        apiKey: options!.apiKey,
        authDomain: options.authDomain,
        databaseURL: options.databaseURL,
        projectId: options.projectId,
        storageBucket: options.storageBucket,
        messagingSenderId: options.messagingSenderId,
        appId: options.appId,
        measurementId: options.measurementId,
      );
    } catch (e) {
      if (_getJSErrorCode(e) == 'app/duplicate-app') {
        throw duplicateApp(name);
      }

      throw _catchJSError(e);
    }
  }

  return _createFromJsApp(app!);
}