Pot<T> class

A class that instantiates and caches an object of type T until it is discarded.

A Pot is a holder that keeps an object instance.

The factory provided to the constructor instantiates an object. It always works in a lazy manner; it does not create an object until it is first needed. The object is cached, so once it is created, a new one is not created unless the pot is reset or the factory is replaced.

The disposer is triggered when the object is disposed by methods such as reset and ReplaceablePot.replace.

final counterPot = Pot<Counter>(
  () => Counter(),
  disposer: (counter) => counter.dispose(),
);

void main() {
  // The factory has not been called yet at this point.
  ...

  // The factory creates a Counter object.
  final counter = counterPot();
  ...

  // The object is discarded and the disposer function is called.
  counterPot.reset();
}

instead of:

final counter = Counter();

It is possible to replace the factory and/or the object with ReplaceablePot.replace.

final counterPot = Pot.replaceable<User>(() => User.none());

void main() {
  counterPot.replace(() => User(id: 100));
}

or with replaceForTesting if the pot is not the one created by Pot.replaceable:

final counterPot = Pot(() => Counter());

void main() {
  Pot.forTesting = true;

  test('Some test', () {
    counterPot.replaceForTesting(() => MockCounter());
  });
}

It is easy to discard the object when it becomes no longer necessary.

final counter = counterPot();
final repository = repositoryPot();
...
Pot.resetAll();

The Pot class also provides the feature of scoping the range where particular objects exist.

// The index of the current scope changes from 0 to 1.
Pot.pushScope();

// A counter object is created in the scope 1.
counterPot.create();

// The object is discarded when the scope 1 is removed.
Pot.popScope();
Implementers
Annotations
  • @sealed

Constructors

Pot(PotObjectFactory<T> factory, {PotDisposer<T>? disposer})
Creates a Pot that instantiates and caches an object of type T until it is discarded.

Properties

hashCode int
The hash code for this object.
no setterinherited
hasObject bool
Whether an object has been created by the factory and still exists.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
scope int?
The index number of the scope that the object of this pot has been bound to.
no setterinherited

Methods

call({bool suppressWarning = false}) → T
Returns the object of type T created by the factory.
inherited
create({bool suppressWarning = false}) → void
Calls the factory to create an object of type T.
inherited
dispose() → void
Discards resources in the pot.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
notifyObjectUpdate() → void
Notifies the listeners of an update of the object in the pot.
inherited
replaceForTesting(PotObjectFactory<T> factory) → void
Replaces the factory set in the constructor with a new one, and/or creates a new object using the new factory, for testing purposes.
inherited
reset() → void
Discards the object of type T that was created by the factory and has been held in the pot.
inherited
toString() String
A string representation of this object.
inherited

Operators

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

Static Properties

currentScope int
The index number of the current scope.
no setter
forTesting bool
The flag that shows whether replaceForTesting is enabled.
getter/setter pair
hasListener bool
Whether there is a listener of Pot events.
no setter

Static Methods

listen(void onData(PotEvent event)) PotListenerRemover
Starts listening for events related to pots.
pending<T>({PotDisposer<T>? disposer}) ReplaceablePot<T>
Creates a pot of type ReplaceablePot where its factory of type T is yet to be set.
popScope() → void
Removes the current scope from the stack of scopes.
pushScope() → void
Adds a new scope to the stack of scopes.
replaceable<T>(PotObjectFactory<T> factory, {PotDisposer<T>? disposer}) ReplaceablePot<T>
Creates a pot of type ReplaceablePot that has the ability to replace its factory with another one of type T.
resetAll({bool keepScopes = true}) → void
Resets all pots of all scopes.
resetAllInScope() → void
Resets all pots in the current scope.