Closed lukepighetti closed 8 months ago
Hey Luke, it would be really easy to add such a widget, but it sort of defeats the purpose of the library, IMO, so I'm quite hesitant about adding it to the lib. Let me explain.
watch
function from the builder parameter would have to both accept and return dynamic
, which is going to be annoying to work with. Extensions on observable types don’t have such a problem and allow for defining the whole different method signatures based on the receiver type. This gets even more important with the v2 of this package, as ‘.watch()’ will now return AsyncSnapshot<T>
for Stream<T>
/Future<T>
, T
for ValueListenable<T>
and the Listenable
instance itself for any Listenable
.WatchBuilder((context) => …)
, which would just do this:class WatchBuilder extends StatelessWidget {
const WatchBuilder({
super.key,
required this.builder,
});
final WidgetBuilder builder;
Widget build(BuildContext context) {
context.unwatch();
return builder(context);
}
}
, but, to me, it looks worse in terms of DX compared to just adding the context.unwatch()
by hand where/if needed. Especially since in v2 this call is even cheaper and can be made anywhere inside the build
method body, even multiple times - with the same result.
context.unwatch()
at_all should_not break anything in terms of app's logic/UI:
.watch()
call is conditionally replaced with another .watch()
call within the build
method..watch()
/context.unwatch()
calls have disappeared from the build
method's body.Something.of(context)
) behavior for every observable type out there + make the package stupidly simple to learn and use. This proposal doesn't align with the mentioned vision, IMO.context_watch
in such a way that everytime you access this "InheritedWidget's alternative" within the build
method - context.unwatch()
will be triggered automatically, making the potential extra rebuilds even less possible, without any additional work from your side. So, if your use-case involves any parent tree lookup for something, you might want to take a look at that new package. I'll let you know when it's released if you're interested.context_watch
v2 will provide a .watchValue(context, (observable) => observable.property)
extension to make a widget rebuild only when the specific property value changes. This might help with getting rid of some conditional .watch()
calls.context_watch 2.0, along with context_ref and context_plus are out now. Check them out to enjoy even less likely unwanted conditional watch()
related rebuilds.
Any thoughts on adding a simple WatchBuilder(builder: (context, watch) => ...) widget that allows subscribing to an arbitrary number of reactive classes (the ones already supported by this package, nice work btw), and also handles the unwatch limitation?