init method

  1. @override
Future<void> init({
  1. Map<String, dynamic>? config,
  2. FirebaseApp? app,
})
override

Initializes the cloud app configuration by setting up Firebase Remote Config.

You must provide either a FirebaseApp instance or a config map with the service account path. If app is provided, it initializes Firebase using that instance. If config is provided, it initializes Firebase using the service file path.

Throws an ArgumentError if both app and config are provided. Throws an Exception if initialization fails.

Implementation

@override
Future<void> init({Map<String, dynamic>? config, FirebaseApp? app}) async {
  if (app != null && config != null) {
    throw ArgumentError('You cannot provide both app and serviceFilePath.');
  }
  if (app != null) {
    try {
      // Initializes FirebaseRemoteConfig with the provided FirebaseApp.
      firebaseRemoteConfig = FirebaseRemoteConfig.instanceFor(app: app);
    } catch (e) {
      throw Exception(
          'Failed to initialize Firebase Remote Config with the provided app: $e');
    }
  } else if (config != null) {
    try {
      // Retrieves the Firebase app instance from the service file and initializes RemoteConfig.
      FirebaseApp app =
          await _cloudThemeManager.getFirebaseAppFromFile(config);

      firebaseRemoteConfig = FirebaseRemoteConfig.instanceFor(app: app);

      // Sets configuration settings for RemoteConfig.
      await firebaseRemoteConfig.setConfigSettings(RemoteConfigSettings(
        fetchTimeout: const Duration(minutes: 1),
        minimumFetchInterval: const Duration(seconds: 1),
      ));

      // Fetches and activates the remote configuration.
      await firebaseRemoteConfig.fetchAndActivate();
    } catch (e) {
      throw Exception(
          'Failed to initialize Firebase using the service account file: $e');
    }
  } else {
    throw ArgumentError(
        'You must provide either a serviceFilePath or an app instance.');
  }
}