escamoteur / watch_it

MIT License
103 stars 8 forks source link

Cannot register handler for ChangeNotifiers #19

Closed smjxpro closed 6 months ago

smjxpro commented 7 months ago

I have a Counter class:

class Counter extends ChangeNotifier {
        int _count = 0;
        int get count=>_count;

        void increment(){
              _count++;
             notifyListeners();
       }
}

Now I want to show a dialog when the value of the count becomes greater than 5. Therefore I want to register a handler in the build method like:

  Widget build(BuildContext context) {
    registerHandler(
        select: (Counter c) => c.count,
        handler: (context, value, cancel) => showNameDialog(context, value));
    ...
  }

But it says count is not a ValueListenable as required by the context or something like that. I can watch it though.

It is cumbersome to wrap every variable in a ValueListenable. And because we can watch it we should also be able to register handlers for it.

escamoteur commented 7 months ago

Ah, I see, you can watch it because the parent class implements ChangeNotifiers otherwise you would not be able to watch it. Not sure how a registerHandler would look like in such a case. It would need a selector. Unfortunately Dart doesn't support method oveoading, so we need another name for such a function. Am 5. Dez. 2023, 15:04 +0100 schrieb S. M. JAHANGIR @.***>:

I have a Counter class: class Counter extends ChangeNotifier { int _count = 0; int get count=>_count;

   void increment(){
         _count++;
        notifyListeners();
  }

} Now I want to show a dialog when the value of the count becomes greater than 5. Therefore I want to register a handler in the build method like: Widget build(BuildContext context) { registerHandler( select: (Counter c) => c.count, handler: (context, value, cancel) => showNameDialog(context, value)); ... } But it says count is not a ValueListenable as required by the context or something like that. I can watch it though. It is cumbersome to wrap every variable in a ValueListenable. And because we can watch it we should also be able to register handlers for it. — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you are subscribed to this thread.Message ID: @.***>

smjxpro commented 7 months ago

Maybe registerChangeNotifierHandler?