initAsync method

Future<bool> initAsync()

Initializes the Firebase Remote Config plugin.

Implementation

Future<bool> initAsync() async {
  // Already called.
  if (_init) {
    return _init;
  }

  try {
    // If explicitly supplied a FirebaseApp
    if (_app != null) {
      _remoteConfig ??= r.FirebaseRemoteConfig.instanceFor(app: _app!);
      _app = null;
    } else {
      // Gets the instance of RemoteConfig for the default Firebase app.
      _remoteConfig ??= r.FirebaseRemoteConfig.instance;
    }

    await _remoteConfig?.setConfigSettings(r.RemoteConfigSettings(
      fetchTimeout: Duration(seconds: _fetchTimeout),
      minimumFetchInterval: Duration(hours: _minimumFetchInterval),
    ));

    if (_defaults != null) {
      await _remoteConfig?.setDefaults(_defaults!);
    }

    _activated = await fetchAndActivate(throwError: true);

    if (_key == null) {
      final info = await PackageInfo.fromPlatform();
      final key = info.packageName.replaceAll('.', '');
      _key = _remoteConfig?.getString(key);
      // Supply the package name as the key
      if (_key == null || _key!.trim().isEmpty) {
        _key = key;
      }
    }

    if (_key == null || _key!.trim().isEmpty) {
      throw ArgumentError('An invalid key provided to RemoteConfig()');
    }

    _crypto = StringCrypt(password: _key);

    _init = true;
    // Fetch throttled.
    // } on r.FetchThrottledException catch (ex) {
    //   _init = false;
    //   getError(ex);
  } catch (ex) {
    _init = false;
    getError(ex);
  }
  return _init;
}