Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Where should I put Computed? #7

Closed
tbm98 opened this issue Jun 23, 2020 · 15 comments
Closed

Where should I put Computed? #7

tbm98 opened this issue Jun 23, 2020 · 15 comments
Assignees
Labels
question Further information is requested

Comments

@tbm98
Copy link
Contributor

tbm98 commented Jun 23, 2020

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 😕

@tbm98 tbm98 added the enhancement New feature or request label Jun 23, 2020
@rrousselGit rrousselGit added question Further information is requested and removed enhancement New feature or request labels Jun 23, 2020
@rrousselGit
Copy link
Owner

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

@tbm98
Copy link
Contributor Author

tbm98 commented Jun 23, 2020

Is there any memory problem if I have many static variables ?

@rrousselGit
Copy link
Owner

No. That's the same thing as using global variables.

@tbm98
Copy link
Contributor Author

tbm98 commented Jun 23, 2020

Yep. Thanks!
I'll use static variables

@tbm98 tbm98 closed this as completed Jun 23, 2020
@kaboc
Copy link

kaboc commented Jun 24, 2020

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)});
}

@rrousselGit
Copy link
Owner

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

@rrousselGit
Copy link
Owner

If you want a "select", there's one for hooks:

useProvider(myProvider.select((value) => value.foo))

@kaboc
Copy link

kaboc commented Jun 24, 2020

OK, I'll think about using hooks, hoping it'll be improved in the future.
Thanks anyway!

@rrousselGit
Copy link
Owner

rrousselGit commented Jun 24, 2020 via email

@tbm98
Copy link
Contributor Author

tbm98 commented Jun 24, 2020

Hi.
I not found select function on provider
Screen Shot 2020-06-24 at 5 08 16 PM

@kaboc
Copy link

kaboc commented Jun 24, 2020

@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.

@kaboc
Copy link

kaboc commented Jun 24, 2020

@tbm98
Have you imported hooks_riverpod correctly ?

@tbm98
Copy link
Contributor Author

tbm98 commented Jun 24, 2020

@kaboc no, but I think select function only in ChangeNotifier

@rrousselGit
Copy link
Owner

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

@kaboc
Copy link

kaboc commented Jun 24, 2020

Got it. I'll tell it to myself again and again until I accept it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants