reset method

void reset()
inherited

Discards the object of type T that was created by the factory and has been held in the pot.

This method triggers the disposer, which was set in the constructor of Pot, if an object exists.

This does not discard the pot itself, so a new object is created again when it is need. Use this when the object is not used any more for now, and get a new object when it is necessary again.

final counterPot = Pot<Counter>(
  () => Counter(0),
  disposer: (counter) => print('Discarded'),
);

void main() {
  var counter = counterPot();
  counter.increment();
  print(counter.value); // 1

  // Discards the existing object that has been held in the pot,
  // and calls the disposer, printing "Discarded".
  counterPot.reset();

  // A new object is created if it is accessed.
  counter = counterPot();
  print(counter.value); // 0
}

Note that calling this method has no effect if the object has not been created.

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

void main() {
  // The disposer is not triggered because there is no object yet.
  counterPot.reset();
}

Implementation

void reset() {
  if (_isDisposed) {
    throwStateError();
  }

  if (_hasObject) {
    _callDisposer();
    _object = null;
    _hasObject = false;
    _scope = null;
    StaticPot.scopes.removePot(_pot);
    StaticPot.eventHandler.addEvent(PotEventKind.reset, pots: [_pot]);
  }
}