initializeApp method

App initializeApp([
  1. AppOptions? options,
  2. String name = defaultAppName
])

Creates and initializes a Firebase App instance with the given options and name.

The options is initialized with provided credential and databaseURL. A valid credential can be obtained with cert or certFromPath.

The name argument allows using multiple Firebase applications at the same time. If omitted then default app name is used.

Example:

var certificate = FirebaseAdmin.instance.cert(
  projectId: 'your-project-id',
  clientEmail: 'your-client-email',
  privateKey: 'your-private-key',
);
var app = FirebaseAdmin.instance.initializeApp(
  AppOptions(
    credential: certificate,
    databaseURL: 'https://your-database.firebase.io')
);

See also:

Implementation

App initializeApp([AppOptions? options, String name = defaultAppName]) {
  options ??= _loadOptionsFromEnvVar(Credentials.applicationDefault());
  if (name.isEmpty) {
    throw FirebaseAppError.invalidAppName(
      'Invalid Firebase app name "$name" provided. App name must be a non-empty string.',
    );
  } else if (_apps.containsKey(name)) {
    if (name == defaultAppName) {
      throw FirebaseAppError.duplicateApp(
        'The default Firebase app already exists. This means you called initializeApp() '
        'more than once without providing an app name as the second argument. In most cases '
        'you only need to call initializeApp() once. But if you do want to initialize '
        'multiple apps, pass a second argument to initializeApp() to give each app a unique '
        'name.',
      );
    } else {
      throw FirebaseAppError.duplicateApp(
        'Firebase app named "$name" already exists. This means you called initializeApp() '
        'more than once with the same app name as the second argument. Make sure you provide a '
        'unique name every time you call initializeApp().',
      );
    }
  }

  return _apps[name] = App(name, options);
}