A tiny state management library for Dart developers.
Essentially a functional approach to flutter's State
except without the flutter.
A simple usage example:
import 'package:stateful/stateful.dart';
class Person {
int age;
String name;
Person(this.name,this.age);
}
main() {
var state = Stateful(Person('Joe Smith',39));
print(state.getState().name); // prints Joe Smith
var unsub = state.addListener((state) => print('${state.name} is ${state.age}')); // prints Joe Smith is 39
state.setState(patch: (person) => person..age = person.age+1); // prints Joe Smith is 40
unsub(); // unsubscribe
state.setState(patch: (person) => person..age = person.age+1); // doesn't print
}
Stolen from reselect. Selectors allow to you select a piece of the state and call a callback only when the selected part of it has changed.
import 'package:stateful/stateful.dart';
class Person {
int age;
String name;
Person(this.name,this.age);
}
main() {
var state = Stateful(Person('Joe Smith',39));
var unsub = state.addListener(
Selector1(
(state) => state.age,
(age) => print('The person is $age')
)
); // prints The person is 39
state.setState(patch: (person) => person..age = person.age+1); // prints The person is 40
state.setState(patch: (person) => person..name = 'Jane Smith'); // doesn't print
}
Dart doesn't support overloading so you need to call a different Selector function (Selector#
) depending on how many dependency functions you have.
Please file feature requests and bugs at the issue tracker.