configure method

Future<void> configure(
  1. String configuration
)

Configures Amplify with the provided configuration string. This method can only be called once, after all the plugins have been added and no plugin shall be added after amplify is configured. Customers are expected to call Amplify.isConfigured to check if their app is configured before calling this method.

Throws AmplifyAlreadyConfiguredException if this method is called again (e.g. during hot reload).

Implementation

Future<void> configure(String configuration) async {
  if (isConfigured) {
    throw const AmplifyAlreadyConfiguredException(
      'Amplify has already been configured and re-configuration is not supported.',
      recoverySuggestion:
          'Check if Amplify is already configured using Amplify.isConfigured.',
    );
  }

  late AmplifyConfig amplifyConfig;
  try {
    try {
      final json = jsonDecode(configuration) as Map;
      amplifyConfig = AmplifyConfig.fromJson(json.cast());
    } on Object {
      throw ConfigurationError(
        'The provided configuration is not a valid json. '
        'Check underlyingException.',
        recoverySuggestion:
            'Inspect your amplifyconfiguration.dart and ensure that '
            'the string is proper json',
      );
    }
    await _configurePlugins(amplifyConfig);
    _configCompleter.complete(amplifyConfig);
  } on ConfigurationError catch (e, st) {
    // Complete with the configuration error and reset the completer so
    // that 1) `configure` can be called again and 2) listeners registered
    // on `asyncConfig` are notified of the error so they can handle it as
    // appropriate. For example, the Authenticator listens for `asyncConfig`
    // to complete before updating the UI.
    _configCompleter.completeError(e, st);
    rethrow;
  } on Object {
    // At this point, configuration is complete in the sense that the
    // configuration file has been validated and plugins have had their
    // `configure` methods run with no `ConfigurationException`s.
    //
    // Any other errors which occur during plugin configuration should be
    // handled by the developer, but since they are unrelated to
    // configuration, listeners to `Amplify.asyncConfig` should be allowed to
    // proceed with the validated configuration.
    _configCompleter.complete(amplifyConfig);
    _configCompleter = Completer();
    rethrow;
  }
}