reinitialize method

  1. @protected
  2. @visibleForTesting
Future reinitialize(
  1. Future future(), {
  2. bool callInitializedWithErrorOnException = true,
})

Allows to reinitialize the object with the call of the given future.

  • future is a future to execute during reinitialization.

    Before executing it, the object will be marked as not initialized. If future throws an exception, it will be rethrown to the caller.

  • callInitializedWithErrorOnException indicates whether to call initializedWithError on Exception or not.

    For example, you can capture the exception, handle it and then decide if initializedWithError should be called or not.

Throws:

Implementation

@protected
@visibleForTesting
Future reinitialize(
  Future Function() future, {
  bool callInitializedWithErrorOnException = true,
}) async {
  if (!isInitialized) {
    throw EnsureInitializedException(wasNotInitializedYetMessage);
  }

  markAsUninitialized();

  try {
    await future();

    initializedSuccessfully();
  } on Exception catch (e, s) {
    if (callInitializedWithErrorOnException) {
      initializedWithError(error: e, stackTrace: s);
    }

    rethrow;
  }
}