AsyncNotifier: how to watch only for a property change, ignore the loading/error states #3971
-
|
Hello I have a StreamProvider that is a Stream of a Firebase snapshot of a document Event. @riverpod
Stream<Mood?> eventStream(Ref ref, EventID evenId) {
final eventsRepository = ref.watch(eventsRepositoryProvider);
return eventsRepository.watchEvent(id: eventId);
}This stream is watched on the Event screen where I need to handle loading/error states wich @riverpod
class ChatController extends _$ChatController {
@override
Future<void> build({required EventID eventdId}) async {
state = AsyncValue.loading();
final members = ref.watch(
eventStreamProvider(eventId).select((value) => value.value?.settings));
...Problem is when the event is edited it transitions for a bit between AsyncLoading and AsyncData causing my ChatController to rebuild. TL;DR how can I in a select for an AsyncProvider ignore loading/error states (and maybe return the previous value) I was thinking maybe I can store the previous value and do something like final members = ref.watch(
eventStreamProvider(moodId).select((value) => value.value != null ? value.value.settings : _storedSettings));Thank you |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
|
You can use ref.listen to manually control the updates to your state |
Beta Was this translation helpful? Give feedback.
-
|
Use the Of course you will get a null before the first value comes out from stream. |
Beta Was this translation helpful? Give feedback.
Use the
members.valueOrNull. the state will keep the previous data even when there is an error.Of course you will get a null before the first value comes out from stream.