With version 1.1.0 now you can create a VarWatcher
instance of any type using the getter .watch
which will make it easy for you to watch your variable. In addition, with 1.1.0 you can build watchers with much simpler methods. For example, you can
invoke .build()
method on your watcher and pass in the builder function.
final watcher = 0.watch;
// This will rebuild automaticlly whenever notify is invoked on watcher
Widget watcherBuilder = watcher.build((value) => Text('value = $value'));
Furthermore, we made builder even simpler by introducing a new operator on VarWatcher
.
Widget simple = watcher >> (value) => Text('value = $value');
Similarly, if you declare a list of VarWatcher
s you can also invoke the same method and operator.
final watchers = [
0.watch,
0.watch,
0.watch,
];
Widget watchersBuilder = watchers.build(
() => Column(children: watchersBuilder.map((w) =>
Text('value = ${w.value}')).toList()));
// or
Widget simple2 = watchers >> Column(children: watchersBuilder.map((w) =>
Text('value = ${w.value}')).toList());
WatcherBuilder is the simplest state management in flutter. All you have to do
is declare a VarWatcher, use it inside a WatcherBuilder and call notify anytime
you want to update/rebuild its widget.
@override
Widget build(BuildContext context) {
final counter = VarWatcher(0);
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
WatcherBuilder(
watch: counter,
builder: (context, value) => Text('Counter = $value'),
),
OutlinedButton(
onPressed: () {
counter.value++;
counter.notify();
},
child: const Text('Increment'),
)
],
),
),
);
}
You can also use WatchersBuilder if you want to watch multiple VarWatchers