ProviderModel<T> class abstract

ProviderModel is an abstract class that can be extended to create a model for state management and safe logic flow for your application. It uses the ChangeNotifier mixin to notify its listeners when changes occur, and therefore can be used with Provider via ChangeNotifierProvider, and anything that can work with ChangeNotifier or Listenable.

It provides methods for creating and managing variables (ProviderModelVariable), locking the model for safe modifications, and subscribing to changes via addListener or use.

The generic parameter T should be the type of the class that extends ProviderModel.

Example usage:

class MyClass extends ProviderModel<MyClass> {
  late final _myVariable = createVariable<int>(0);

  int get myVariable => _myVariable.unsafeValue;

  void increment() {
    // This will increment _myVariable by 1.
    // If this function is called multiple times at the same time,
    // the value will be incremented 1 by 1 sequentially.
    lock((batch) async {
      // Do some long running API call.
      await longApiCall();
      batch.set(_myVariable, batch.get(_myVariable) + 1);
      batch.commit();
    });
  }
}

// Some build function of some widget with MyClass being provided
// via ChangeNotifierProvider.
Widget build(BuildContext context) {
  final tValue = context.watch<MyClass>().myVariable;

  return TextButton(
    onPressed: () {
      context.read<MyClass>().increment();
    },
    child: Text('$tValue'),
  );
}
Mixed-in types
Implementers

Constructors

ProviderModel.new()

Properties

disposed bool
Whether this ProviderModel has been disposed or not.
no setter
hashCode int
The hash code for this object.
no setterinherited
hasListeners bool
Whether any listeners are currently registered.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

addListener(VoidCallback listener) → void
Register a closure to be called when the object changes.
inherited
begin([ProviderLockKey? lockKey]) ProviderModelBatch
Create a batch to safely modify this ProviderModel's ProviderModelVariable members.
createUnsetVariable<U>() ProviderModelVariable<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.
createVariable<U>(U initial) ProviderModelVariable<U>
Create a ProviderModelVariable belonging to this ProviderModel that can be used in batches and automatically detect changes to notify listeners when it's value changes.
dispose() → void
Dipose this ProviderModel. This will call dispose on all ProviderModelVariables created by this ProviderModel. This will also cancel all ProviderModelUseSubscriptions created by this ProviderModel.
override
lock(ProviderModelProcess process, {ProviderLockKey? lockKey, bool override = false, bool overridable = false, FutureOr<void> onOverride()?, bool waitForMicrotasks = true, bool waitForTimers = false, bool waitForPeriodicTimers = false}) Future<bool>
Queue up to lock this ProviderModel to safely modify it's ProviderModelVariable members. The process passed here is guarunteed to be executed sequentially and non-parallel to other proccesses passed to lock.
locked(ProviderLockKey lockKey) bool
Whether this ProviderModel is locked with lockKey or not.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
notifyListeners() → void
Call all the registered listeners.
inherited
removeListener(VoidCallback listener) → void
Remove a previously registered closure from the list of closures that are notified when the object changes.
inherited
toString() String
A string representation of this object.
inherited
use(void callback(T model)) ProviderModelUseSubscription<T>
Subscribe to changes to this ProviderModel. The callback passed here will be called whenever a ProviderModelVariable belonging to this ProviderModel that was accessed inside the callback changes. The callback will be called immediately once during execution of this function synchronously.

Operators

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