ensureInitialized method
Ensures something was initialized or initializes it.
If initialization was started before and it is not completed, returns future of it's ending.
If initialize in it throws, then object is means to be uninitialized, and this method can be called again. The error of initialize will caught and rethrow.
Returns future of initialization ending or nothing if initialized.
Implementation
Future<void> ensureInitialized() async {
if (_state == _initializedState) {
return;
}
if (_state == _initializingState) {
return _completer!.future;
}
bool resetDoNotAssert() {
_isInitializingAssertionDisabled = false;
return true;
}
_completer = Completer<void>();
assert(() {
_isInitializingAssertionDisabled = true;
return true;
}());
try {
_state = _initializingState;
await initialize();
_state = _initializedState;
_completer!.complete();
_completer = null;
} catch (e) {
_state = _uninitializedState;
_completer!.completeError(e);
_completer = null;
rethrow;
} finally {
assert(resetDoNotAssert());
}
}