instanceFor static method

FirebaseStorage instanceFor({
  1. FirebaseApp? app,
  2. String? bucket,
})

Returns an instance using a specified FirebaseApp and/or custom storage bucket.

If app is not provided, the default Firebase app will be used. If bucket is not provided, the default storage bucket will be used.

Implementation

static FirebaseStorage instanceFor({
  FirebaseApp? app,
  String? bucket,
}) {
  app ??= Firebase.app();

  if (bucket == null && app.options.storageBucket == null) {
    if (app.name == defaultFirebaseAppName) {
      _throwNoBucketError(
          'No default storage bucket could be found. Ensure you have correctly followed the Getting Started guide.');
    } else {
      _throwNoBucketError(
          "No storage bucket could be found for the app '${app.name}'. Ensure you have set the [storageBucket] on [FirebaseOptions] whilst initializing the secondary Firebase app.");
    }
  }

  String _bucket = bucket ?? app.options.storageBucket!;

  // Previous versions allow storage buckets starting with "gs://".
  // Since we need to create a key using the bucket, it must not include "gs://"
  // since native does not include it when requesting the bucket. This keeps
  // the code backwards compatible but also works with the refactor.
  if (_bucket.startsWith('gs://')) {
    _bucket = _bucket.replaceFirst('gs://', '');
  }

  String key = '${app.name}|$_bucket';
  if (_cachedInstances.containsKey(key)) {
    return _cachedInstances[key]!;
  }

  FirebaseStorage newInstance = FirebaseStorage._(app: app, bucket: _bucket);
  _cachedInstances[key] = newInstance;

  return newInstance;
}