createUnsetVariable<U> method

  1. @nonVirtual
  2. @protected
ProviderModelVariable<U> createUnsetVariable<U>()

Creates an unset ProviderModelVariable belonging to this ProviderModel that can be used in batches and automatically detect changes to notify listeners when it's value changes.

When a variable is 'unset', it's value cannot be accessed until it is set via a ProviderModelBatch.set and ProviderModelBatch.commit or an assertion with be thrown.

Example:

class MyClass extends ProviderModel<MyClass> {
  late final myVariable = createUnsetVariable<int>();
}

final model = MyClass();

model.myVariable.unsafeValue; // assertion error

model.begin()
  ..set(model.myVariable, 3)
  ..commit();

model.myVariable.unsafeValue; // 3

Implementation

@nonVirtual
@protected
ProviderModelVariable<U> createUnsetVariable<U>() {
  assert(
    _debugValidType ??= T != const _DebugDynamicType<dynamic>().type,
    'ProviderModel\'s T type parameter must be the same as the class that extends it. Currrently it is dynamic or unset.',
  );

  final tContainer = ProviderModelVariable<U>._internalCreate(
    this,
    null,
    false,
  );

  _variables.add(tContainer);

  return tContainer;
}