pushScope static method

void pushScope()

Adds a new scope to the stack of scopes.

A "scope" here is a notion related to the lifespan of an object created by and held in a pot.

For example, if the index number of the current scope is 2 and the factory set in the constructor is triggered for the first time, an object is created by the factory and gets bound to the scope 2. The object exists while the current scope is 2 or newer, so it is discarded when the scope 2 is removed.

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

void main() {
  // A new scope is added, and the `currentScope` turns 1.
  // The object is not bound to the scope yet because it hasn't created.
  Pot.pushScope();

  // An object is created and set in the current scope.
  final counter = counterPot();

  // The object is discarded.
  // The scope 1 is removed and the `currentScope` turns 0.
  Pot.popScope();
}

Implementation

static void pushScope() {
  StaticPot.scopes.createScope();
}