handleInitEvent method

Stream<FastFirebaseRemoteConfigBlocState> handleInitEvent(
  1. Map<String, dynamic>? defaultConfig
)

Implementation

Stream<FastFirebaseRemoteConfigBlocState> handleInitEvent(
  Map<String, dynamic>? defaultConfig,
) async* {
  if (canInitialize) {
    isInitializing = true;
    yield currentState.copyWith(isInitializing: true);

    final remoteConfig = FirebaseRemoteConfig.instance;
    await remoteConfig.ensureInitialized();

    _logger.debug('setting config settings');
    await remoteConfig.setConfigSettings(RemoteConfigSettings(
      minimumFetchInterval: const Duration(hours: 1),
      fetchTimeout: const Duration(minutes: 1),
    ));

    if (defaultConfig != null) {
      _logger.debug('setting default config: $defaultConfig');
      await remoteConfig.setDefaults(defaultConfig);
    }

    bool activated = false;

    try {
      // TODO: listen on config changes ?
      _logger.debug('fetching and activating remote config');
      activated = await remoteConfig.fetchAndActivate();
    } catch (error, stackTrace) {
      _logger.error(
        'An error occured when fetching and activating remote config: $error',
        stackTrace,
      );
    }

    _logger.info('remote config is activated', activated);

    final config = remoteConfig.getAll();
    await _updateAppFeatures(config);

    addEvent(
      FastFirebaseRemoteConfigBlocEvent.initialized(activated: activated),
    );
  }
}