Closed ebelevics closed 2 years ago
From https://github.com/rrousselGit/river_pod/issues/705 I found that there is such Ref
as AutoDisposeRef
. So it somewhat answers my question.
~Still kind of don't understand, why Ref is not present in StateNotifier already. My first thought, isn't it possible to check in runtime is provider autoDispose or normal?~ (StateNotifier comes from package:state_notifier)
But I can close topic as there was answered similar topic. Needs to be documented in website.
Still now I'm trying to find out, can I do similar approach as Kotlin LiveData example. Making StateProviders in class static won't work as I want all StateProviders to be tied to ItemsViewModel instance and autodispose as itemsProvider get disposed.
Is your feature request related to a problem? Please describe. I'll give brief example
The problem that triggers me, that I have no way to listen to values, without
ref.watch(webSocketStreamProvider)
rebuilding ChatNotifier during every WS event.ref.read()
is ignored as it doesn't listen to values.ref.listen
kind of does the same asref.watch
, because of return. The only way I somewhat managed to solve is byreturn ChatNotifier(ref: ref);
and usingref.listen
insideChatNotifier
. When I use ref.watch inside 'ChatNotifier' I get weird exceptions.Describe alternatives you've considered Of course I could use ref.listen if I passed ref as
AutoDisposeStateNotifierProviderRef<ChatNotifier, AsyncValue<ChatState>>
notRef
, or otherwise I getThe argument type 'AutoDisposeStreamProvider<WsEvent>' can't be assigned to the parameter type 'AlwaysAliveProviderListenable<Object>'
Describe the solution you'd like Being able to use
Ref ref
insideStateNotifier
to not only read but also listen to values, or maybe similar toref.read
asfinal Reader read
butref.listen
asfinal RefListener listen
(also I don't really understand, whyRef
is not built intoStateNotifier
similar asstate
is built it. Like providers in general always have ref to pass intoStateNotifier
. Why this additional step is required?Additional context Recently I have been working on Android Studio Kotlin project, and one thing that IMO kotlin did a lot better is managing data with ViewModels by using LiveData. And Riverpod in that sense looked like it had similar possibility. So I tried to do similar from
to [Psuedo-code]
and theorotically then in widget to initiate viewModel you do the usual
and to watch value from other "LiveData" you use
but it seems currently it is hard to achieve something like that, or maybe it's possible. I don't know I haven't tried to inspecting riverpods implementation much more closely and modifying to expect this behaviour