escamoteur / watch_it

MIT License
103 stars 8 forks source link

registerHandler not working with ValueNotifier #28

Closed martyfuhry closed 3 months ago

martyfuhry commented 3 months ago

If I have a local ValueNotifier and attempt to use registerHandler, I get an error:

Invalid argument(s): Either the return type of the select function or the type T has to be a Listenable

This tracks down to the following check in registerHandler:

    if (T is Listenable) {
      observedObject = (parentObject) as Listenable;
    } else {
      throw ArgumentError(
          'Either the return type of the select function or the type T has to be a Listenable');
    }

I'm using it like this:

class MyWidget extends WatchingStatefulWidget {
...
}

class _MyWidgetState extends State<MyWidget> {
  final ValueNotifier<int> _myValueNotifier = ValueNotifier(0);

  @override
  Widget build(BuildContext context) {
    registerHandler(
      target: _myValueNotifier,
      handler: (context, value, cancel) {
        print('value is $value');
      }
    );
    ...
  }
}

But this is probably the wrong thing to check. In this case,

print(ValueNotifier is Listenable); // prints false
print(_myValueNotifier is Listenable); // prints true

I have no earthly idea why ValueNotifier is Listenable evaluates to false, but it certainly does.

escamoteur commented 3 months ago

ok, the reason is explained here, I have to fix this I too was expecting it to work that way

https://github.com/dart-lang/sdk/issues/43390

escamoteur commented 3 months ago

fixed in V1.4.1