As far as I know, Computed is like a filter and it only calculates and notifies Consumer when there's a change. So in case I have a complex state, I need a lot of Computed, should I set my Computed as global variables or set it as static variables or something else?
If I set as global variables, I can't remember them all 😕
You could set them as static variables on your Model
Like:
class MyNotifier extends StateNotifier {
static final myComputed =Computed(...)
}
Or keep them all in the same file
Is there any memory problem if I have many static variables ?
No. That's the same thing as using global variables.
Yep. Thanks!
I'll use static variables
What if it's not global nor static?
For example, is it also fine to use Computed in every method where the value is necessary, like the way we use select() of the provider package?
My concern is especially whether the computed is disposed of and stops listening properly at/after the end of each method scope.
class Counter with ChangeNotifier {
int value1;
int value2;
...
}
...
@override
Widget build(BuildContext context) {
final computed = Computed((read) => read(counterProvider).value1);
return Text('value: ${read(computed)});
}
Bad idea, at least for now.
A Computed isn't fully destroyed when no-longer used. It'll likely change in the future, but for now you'd have a leak
If you want a "select", there's one for hooks:
useProvider(myProvider.select((value) => value.foo))
OK, I'll think about using hooks, hoping it'll be improved in the future.
Thanks anyway!
What is your need to begin with?
On Wed, 24 Jun 2020, 10:33 Kabo, notifications@github.com wrote:
OK, I'll think about using hooks, hoping it'll be improved in the future.
Thanks anyway!—
You are receiving this because you were assigned.
Reply to this email directly, view it on GitHub
https://github.com/rrousselGit/river_pod/issues/7#issuecomment-648710190,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AEZ3I3IBTGEWJBHJWSD5J6LRYHB53ANCNFSM4OF3DZ6Q
.
Hi.
I not found select function on provider

@rrousselGit
My notifiers extending ChangeNotifier (not StateNotifier) have more than one properties, so I use select() to pick out only part of them for efficiency, preventing unnecessary rebuilds. If the same control is not possible with Computed, all computed values have to be declared globally or as static properties of some class(es), which will end up in having a long declaration list of Computed...
But it may not be so bad. Probably I'm just having some difficulty, at this early stage, accepting the fact that Computed and select() seem similar but are used differently. I'll get used to it soon.
@tbm98
Have you imported hooks_riverpod correctly ?
@kaboc no, but I think select function only in ChangeNotifier
But it may not be so bad. Probably I'm just having some difficulty, at this early stage, accepting the fact that Computed and select() seem similar but are used differently. I'll get used to it soon.
Computed is cached and shared
select is local and not cached
Got it. I'll tell it to myself again and again until I accept it.