get_it_mixin
get_it_mixin copied to clipboard
`Watch` does not trigger rebuild
I have a custom object Strokes
which implements changeNotifier
.
I watch it with:
final Strokes strokes = watchOnly((Strokes s) => s);
This object has two methods deleteLastStroke()
:
// get all strokes except for the last one
var p = _path.computeMetrics().take(_path.computeMetrics().length - 1);
var newPath = Path();
// copy the strokes to a new Path
p.forEach((element) {
newPath.addPath(element.extractPath(0, double.infinity), Offset.zero);
});
_path = newPath;
decrementStrokeCount();
notifyListeners();
and deleteAllStrokes()
:
_path.reset();
_strokeCount = 0;
notifyListeners();
Both of them call notifyListeners() however only deleteAllStrokes()
triggeres a rebuild of the UI.
When calling deleteLastStroke()
nothing happens. However when doing:
deleteLastStroke();
setState((){});
The UI rebuilds and everything works as expected.
If I am doing something wrong could you please point me in the right direction?
did you find the problem?
No, but I think it was a problem with how I used the library. I switched to provider and therefore closed this issue.
ok, sorry I couldn't help you quicker
Same.
// doesn't rebuild
Widget build(BuildContext context) {
final notifier = watchOnly((SomeChangeNotifier v) => v);
...
// rebuild
Widget build(BuildContext context) {
final someField = watchOnly((SomeChangeNotifier v) => v.someField);
...
I think you would need to implement Equatable
for this to work. It's watching the results of the method, and rebuilding when the results change. So it needs oldV != v
for rebuild to trigger.
@escamoteur Does this package even support watching an entire ChangeNotifer
? I assumed it did, but maybe I was wrong.
Sure it does. I m currently only on my phone. Am 22. März 2022, 19:10 +0100 schrieb Shawn @.***>:
I think you would need to implement Equatable for this to work. It's watching the results of the method, and rebuilding when the results change. @escamoteur Does this package even support watching an entire ChangeNotifer? I assumed it did, but maybe I was wrong. — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.Message ID: @.***>
@escamoteur
I just tested. It doesn't work if watchOnly returns a ChangeNotifer
.
It only works when watchOnly doesn't return the ChangeNotifer
instance.
final count = watchOnly((MyChangeNotifier myChangeNotifier) => myChangeNotifier.count, instanceName: "myChangeNotifier"); // triggers build on change
final myChangeNotifier = watchOnly((MyChangeNotifier myChangeNotifier) => myChangeNotifier, instanceName: "myChangeNotifier"); // doesn't trigger build on change
Working workaround:
class _Wrapper<T> {
final T _value;
const _Wrapper(this._value);
}
final count = watchOnly((MyChangeNotifier myChangeNotifier) => _Wrapper(myChangeNotifier), instanceName: "myChangeNotifier")._value;
Sure it does. I m currently only on my phone. Am 22. März 2022, 19:10 +0100 schrieb Shawn @.>: … I think you would need to implement Equatable for this to work. It's watching the results of the method, and rebuilding when the results change. @escamoteur Does this package even support watching an entire ChangeNotifer? I assumed it did, but maybe I was wrong. — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.Message ID: @.>
Can we see an example of how to watch an entire ChangeNotifier ?
Working workaround:
class _Wrapper<T> { final T _value; const _Wrapper(this._value); } final count = watchOnly((MyChangeNotifier myChangeNotifier) => _Wrapper(myChangeNotifier), instanceName: "myChangeNotifier")._value;
for this to work I had remove _value in the end of watchOnly. As _value was retaining the same hashcode
class WatchWrapper<T> {
final T _value;
WatchWrapper(this._value);
T get value => _value;
}
var env = watchOnly((EnvironmentModel m) => WatchWrapper(m));
env.value.something
Issue still exists. @escamoteur can you please fix it? 😃
I had to check in the source, but IMHO if you just pass null for the selector function it will watch the full ChangeNotifier. I'm currently working on the successor to that package and will try to make the API easier.
check out https://github.com/escamoteur/watch_it/issues/1
I recomment switching to my new package watch_it