using ref.keepAlive() vs @Riverpod(keepAlive: true) #3584
-
Hi everyone, I have a question about using I'm wondering if these two approaches achieve the same effect. Specifically, are they functionally equivalent? For example, consider the following implementation using class GuestRepository extends _$GuestRepository {
@override
Future<GuestMap?> build() async {
ref.keepAlive();
// Additional logic here
}
} And this implementation using the @Riverpod(keepAlive: true)
class GuestRepository extends _$GuestRepository {
@override
Future<GuestMap?> build() async {
// Additional logic here
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
Yes, they should achieve the same effect. |
Beta Was this translation helpful? Give feedback.
-
It's the same functionally, but isn't there a difference in memory? @AhmedLSayed9 |
Beta Was this translation helpful? Give feedback.
-
In terms of how they impact your app, both work the same. But one of them empowers riverpod_lint to tell you when you're doing something wrong. Also, be careful with
If you write: @riverpod
Stream<Model> example(ref) async* {
ref.keepAlive();
} That's like if you did: @riverpod
Future<Model> example(ref) async {
await null;
ref.keepAlive();
} So there's a small chance your provider gets disposed before |
Beta Was this translation helpful? Give feedback.
@Riverpod(keepAlive: true)
can be used by the linter to know if a provider is always kept alive.But
ref.keepAlive()
can'tIn terms of how they impact your app, both work the same. But one of them empowers riverpod_lint to tell you when you're doing something wrong.
Also, be careful with
ref.keepAlive()
+async*
async*
implies anawait null
at its start.If you write:
That's like if you did:
So there's a small chance your provider gets disposed before
ref.keepAlive
is reached.