EnsureInitializedResultMixin<T> 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 EnsureInitializedResultMixin<int> {
  SomeClass() {
    _init();
  }

  Future<int> _heavyComputations() async {
    await Future.delayed(const Duration(seconds: 5));

    return 0;
  }

  Future<void> _init() async {
    try {
      final result = await _heavyComputations();

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

Properties

ensureInitialized Future<T>
Released when initializedSuccessfully is called. Returns the result of initialization. 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<T>
Fired when initializedSuccessfully is called. The event would be the result of initialization. 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(T result) → 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<T> 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