How to listen to partial state changes (i.e. select
) without hooks?
#564
-
I'm using enum TimerStatus {
running,
paused,
// Stopped preemptively by the user.
stopped,
// Stopped naturally, i.e. it run to the end.
completed,
}
@immutable
class TimerState {
final Duration remaining;
final TimerStatus status;
const TimerState(
this.remaining,
this.status,
);
} The notifier and provider look like this: class TimerNotifier extends StateNotifier<TimerState> {
// ...
}
final timerNotifierProvider = StateNotifierProvider.autoDispose
.family<TimerNotifier, TimerState, Duration>(
(ref, interval) {
// ...
},
); I tried using the final timerStatus = watch(
timerNotifierProvider(widget.interval).select((value) => value.status),
); but I can't get the code to compile with:
Is this supposed to work? Anyways, I made it work using a second provider that does the selection itself: final timerStatusProvider =
Provider.autoDispose.family<TimerStatus, Duration>(
(ref, interval) => ref.watch(timerNotifierProvider(interval)).status,
); and it works, but seems like quite some boilerplate to need to reimplement what |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
What version of riverpod are you running? Riverpod 1.0.0-dev supports |
Beta Was this translation helpful? Give feedback.
What version of riverpod are you running? Riverpod 1.0.0-dev supports
select
without the need of hooks.