rrousselGit / state_notifier

ValueNotifier, but outside Flutter and with some extra perks
MIT License
311 stars 28 forks source link

Use as Listenable ? #41

Closed tzvc closed 3 years ago

tzvc commented 3 years ago

Hi there,

I'm experimenting with using StateNotifier through a StateNotifierProvider to drive a synced animation in my app. As the doc say StateNotifier is a "fork" of ValueNotifier, I was hoping I could use it directly in place of a Listenable in a AnimatedBuilder like so:


riverpod.Consumer(builder: (BuildContext context, riverpod.ScopedReader watch, Widget child) {
    final navH = watch(vavBarHorizontalValueProvider); // a StateNotifierProvider
    return AnimatedBuilder(
        animation: navH,
        builder: (context, _) {
            return Container(height: navH.value, width:navH.value); 
    });
})

ChangeNotifier extends Listenable so I could change it to a ChangeNotifierProvider but that's more boilerplate code. Is there any way to fix this? And is this solution viable at all?

rrousselGit commented 3 years ago

This is not possible. Why do you need that?

Le jeu. 19 nov. 2020 à 13:44, Théo champion notifications@github.com a écrit :

Hi there,

I'm experimenting with using StateNotifier through a StateNotifierProvider to drive a synced animation in my app. As the doc say StateNotifier is a "fork" of ValueNotifier, I was hoping I could use it directly in place of a Listenable in a AnimatedBuilder like so:

riverpod.Consumer(builder: (BuildContext context, riverpod.ScopedReader watch, Widget child) { final navH = watch(vavBarHorizontalValueProvider); // a StateNotifierProvider return AnimatedBuilder( animation: navigationBloc.verticalBottomNavBarValue, builder: (context, ) { return Container(height: navH.value, width:navH.value); }); })

ChangeNotifier extends Listenable so I could change it to a ChangeNotifierProvider but that's more boilerplate code. Is there any way to fix this? And is this solution viable at all?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/rrousselGit/state_notifier/issues/41, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEZ3I3LWA3REQXTABQXG6ZDSQUOL7ANCNFSM4T3NL74Q .

tzvc commented 3 years ago

@rrousselGit so I can use it to drive an animation like so:

riverpod.Consumer(builder: (BuildContext context, riverpod.ScopedReader watch, Widget child) {
    final navH = watch(vavBarHorizontalValueProvider); // a StateNotifierProvider
    return AnimatedBuilder(
        animation: navH, // this doesnt work, StateNotifier doesnt extend the required Listenable
        builder: (context, _) {
            return Container(height: navH.value, width:navH.value); 
    });
})

The code above works if I replace the StateNotifierProvider by a ChangeNotifierProvider but its just a little bit more boilerplate code :)

rrousselGit commented 3 years ago

If you are using Riverpod, you do not need an AnimatedBuilder to rebuild your widget when the state changes. watch(myProvider.state) takes care of that

StateNotifier cannot implement Listenable. It is not possible

lucavenir commented 1 year ago

Hi @rrousselGit , I do realize this is an old conversation, but...

StateNotifier cannot implement Listenable. It is not possible

... why? I can't answer this question myself. This is just pure curiosity.