reinitialize method
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:
- EnsureInitializedException if object was not initialized yet.
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;
}
}