watch<T extends Listenable> function
- T target
The Watch functions:
The watch functions are the core of this library. They allow you to observe any Listenable, Stream or Future and trigger a rebuild of your widget whenever the watched object changes.
ChangeNotifier based example:
// Create a ChangeNotifier based model
class UserModel extends ChangeNotifier {
get name = _name;
String _name = '';
set name(String value){
_name = value;
notifyListeners();
}
...
}
// Register it
di.registerSingleton<UserModel>(UserModel());
// Watch it
class UserNameText extends WatchingWidget {
@override
Widget build(BuildContext context) {
final userName = watchPropertyValue((UserModel m) => m.name);
return Text(userName);
}
}
there are the following functions:
- watch - observes any Listenable you have access to
- watchIt - observes any Listenable registered in get_it
- watchValue - observes a ValueListenable property of an object registered in get_it
- watchPropertyValue - observes a property of a Listenable object and trigger a rebuild whenever the Listenable notifies a change and the value of the property changes
- watchStream - observes a Stream and triggers a rebuild whenever the Stream emits a new value
- watchFuture - observes a Future and triggers a rebuild whenever the Future completes
To be able to use the functions you have either to derive your widget from WatchingWidget or WatchingStatefulWidget or use the WatchItMixin in your widget class.
To use the watch functions you have to call them inside the build function of
a WatchingWidget or WatchingStatefulWidget or a class that uses the
WatchItMixin. They basically allow you to avoid having to clutter your
widget tree with ValueListenableBuilder, StreamBuilder or FutureBuilder
widgets. Making your code more readable and maintainable.
The functions in detail:
watch observes any Listenable and triggers a rebuild whenever it notifies
a change. That listenable could be passed in as a parameter or be accessed via
get_it. Like final userName = watch(di<UserManager>()).userName; if UserManager is
a Listenable (eg. ChangeNotifier).
if any of the following functions don't fit your needs you can probably use
this one by manually providing the Listenable that should be observed.
Implementation
/// The functions in detail:
/// [watch] observes any Listenable and triggers a rebuild whenever it notifies
/// a change. That listenable could be passed in as a parameter or be accessed via
/// get_it. Like `final userName = watch(di<UserManager>()).userName;` if UserManager is
/// a Listenable (eg. ChangeNotifier).
/// if any of the following functions don't fit your needs you can probably use
/// this one by manually providing the Listenable that should be observed.
T watch<T extends Listenable>(T target) {
assert(_activeWatchItState != null,
'watch can only be called inside a build function within a WatchingWidget or a widget using the WatchItMixin');
_activeWatchItState!.watchListenable(parentOrListenable: target);
return target;
}