AsyncMemoizer<T> class

A class for running an asynchronous function exactly once and caching its result.

An AsyncMemoizer is used when some function may be run multiple times in order to get its result, but it only actually needs to be run once for its effect. To memoize the result of an async function, you can create a memoizer outside the function (for example as an instance field if you want to memoize the result of a method), and then wrap the function's body in a call to runOnce.

This is useful for methods like close() and getters that need to do asynchronous work. For example:

class SomeResource {
  final _closeMemo = new AsyncMemoizer();

  Future close() => _closeMemo.runOnce(() {
    // ...
  });
}

Constructors

AsyncMemoizer()

Properties

future Future<T>
The future containing the method's result. [...]
read-only
hasRun bool
Whether runOnce has been called yet.
read-only
hashCode int
The hash code for this object. [...]
read-only, inherited
runtimeType Type
A representation of the runtime type of the object.
read-only, inherited

Methods

runOnce(FutureOr<T> computation()) Future<T>
Runs the function, computation, if it hasn't been run before. [...]
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed. [...]
inherited
toString() String
Returns a string representation of this object.
inherited

Operators

operator ==(dynamic other) bool
The equality operator. [...]
inherited