How to lazy load data and make it valid throughout the entire lifecycle of the page #3974
Replies: 3 comments 6 replies
-
|
The effect I want is: |
Beta Was this translation helpful? Give feedback.
-
|
Riverpod is always lazy load. Another option is find a page last long enough and watch/listen the provider there. As long as the page(widget) live, your provider will live. |
Beta Was this translation helpful? Give feedback.
-
|
You probably need caching mechanism that A simple solution is to use a global variable @riverpod
Future<String> demoContent(Ref ref, int type) async {
var content = _cache[type];
if (content == null) {
await Future.delayed(Duration(seconds: 2));
content = Future.value("content of type $type");
_cache[type] = content;
}
return content;
}This is valid only if demoContent is used only by DemoWidget since the cache is tied to the lifecycle of the widget. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I have a provider with parameters(such as 1,2,3).
The page has three buttons, and when you click the first button, ref.watch(myProvider(1)) is called, and when you click the second button, ref.watch(myProvider(2)) is called. But when invoke ref.watch(myProvider(2)), the myProvider with param 1 is disposed.
Beta Was this translation helpful? Give feedback.
All reactions