EnsureInitialized mixin

A mixin that allows to track whether the object is ready for usage.

Sometimes it is nice to wait for some heavy initialization process before using the object. Instead of implementing some kind of booleans, we can use futures instead.

Example:

class SomeClass with EnsureInitialized {
  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) {
      unableToInitialize(e, s);
    }
  }
}
  1. @Deprecated('Use EnsureInitializedMixin instead')

Properties

ensureInitialized Future<void>
no setter
hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

initializedSuccessfully() → void
The method that marks the object has been initialized successfully.
initializedWithError({Object? error, String? message, StackTrace? stackTrace}) → void
The method that marks the object was initialized with an error.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited