EnsureInitializedMixin mixin
Allows to track whether the object is ready for usage.
Sometimes it is nice to wait for some heavy initialization process before using an object. Instead of implementing some kind of booleans, we can use futures instead.
Example:
class SomeClass with EnsureInitializedMixin {
SomeClass() {
_init();
}
Future<void> _heavyComputations() async {
await Future.delayed(const Duration(seconds: 5));
}
Future<void> _init() async {
try {
await _heavyComputations();
initializedSuccessfully();
} on Exception catch (e, s) {
initializedWithError(error: e, stackTrace: s);
}
}
}
Properties
-
ensureInitialized
→ Future<
void> -
Released when initializedSuccessfully is called.
If initializedWithError is called, throws the given exception.
no setter
- hashCode → int
-
The hash code for this object.
no setterinherited
- isInitialized → bool
-
Simply checks if the object is initialized at the moment.
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
-
whenInitialized
→ Stream<
void> -
Fired when initializedSuccessfully is called.
If initializedWithError is called, the given exception is passed to the
onError
callback.no setter -
whenUninitialized
→ Stream<
void> -
Fired when markAsUninitialized is called.
no setter
Methods
-
initializedSuccessfully(
) → void - Marks that the object has been initialized successfully.
-
initializedWithError(
{Object? error, String? message, StackTrace? stackTrace}) → void - Marks that the object was initialized with an error.
-
markAsUninitialized(
) → void - Marks that the object is again not initialized. After this, you can call initializedSuccessfully again.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
reinitialize(
Future future(), {bool callInitializedWithErrorOnException = true}) → Future - Allows to reinitialize the object with the call of the given future.
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited