-
-
Notifications
You must be signed in to change notification settings - Fork 974
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
Suggestions for improving documentation on keepAlive #3880
Comments
The behavior of the example is intended. If the provider can be disposed before the request completed, that likely makes more sense. |
Yes, this behavior is as expected. This is not a bug report submission. It is important to note that if keepAlive is set after a Ref is disposed, it will not be reflected. If we use The following log is output to the Provider that invoked keepAlive after
If keepAlive is called before
Sample application and providersclass MyPage extends ConsumerWidget {
const MyPage({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
return Scaffold(
appBar: AppBar(
title: const Text('Example keepAlive'),
),
body: Center(
child: Column(
spacing: 16,
children: [
FilledButton(
onPressed: () async {
print('readAndKeepAlive:Button: started');
final value = await ref.read(
readAndKeepAliveProvider.future,
);
print('readAndKeepAlive:Button: finished: $value');
},
child: const Text('Read readAndKeepAlive'),
),
FilledButton.tonal(
onPressed: () async {
print('readAndFailedKeepAlive:Button: started');
final value = await ref.read(
readAndFailedKeepAliveProvider.future,
);
print('readAndFailedKeepAlive:Button: finished: $value');
},
child: const Text('Read readAndFailedKeepAlive'),
),
],
),
),
);
}
}
const keepDuration = Duration(
seconds: 10,
);
Future<String> computeValue(String value) async {
print('computeValue: Started');
await Future.delayed(
const Duration(
seconds: 5,
),
);
print('computeValue: Finished');
return value;
}
@riverpod
Future<String> readAndKeepAlive(Ref ref) async {
print('readAndKeepAlive:Provider: Started');
ref
..onCancel(() {
print('readAndKeepAlive:Provider: Canceled');
})
..onResume(() {
print('readAndKeepAlive:Provider: Resumed');
})
..onDispose(() {
print('readAndKeepAlive:Provider: Disposed');
});
final link = ref.keepAlive();
final String result;
try {
result = await computeValue('Read and keep alive');
print('readAndKeepAlive:Provider: Get result');
} on Exception catch (_) {
link.close();
rethrow;
}
print('readAndKeepAlive:Provider: Finished');
return result;
}
@riverpod
Future<String> readAndFailedKeepAlive(Ref ref) async {
print('readAndFailedKeepAlive:Provider: Started');
ref
..onCancel(() {
print('readAndFailedKeepAlive:Provider: Canceled');
})
..onResume(() {
print('readAndFailedKeepAlive:Provider: Resumed');
})
..onDispose(() {
print('readAndFailedKeepAlive:Provider: Disposed');
});
final result = await computeValue('Read and failed keep alive');
print('readAndFailedKeepAlive:Provider: Get result');
ref.keepAlive();
print('readAndFailedKeepAlive:Provider: Finished');
return result;
} Again, since Riverpod is centered on |
Describe what scenario you think is uncovered by the existing examples/articles
link: https://riverpod.dev/docs/essentials/auto_dispose#fine-tuned-disposal-with-refkeepalive
If keepAlive is implemented as described, it may not be cached as expected when caching with
ref.read
.Implemented according to the documentation, the code is as follows.
Calling it with
ref.read
will result in the following output.Describe why existing examples/articles do not cover this case
This problem can be solved by doing keepAlive before the await process, as in the
cacheFor
extension.https://riverpod.dev/docs/essentials/auto_dispose#example-keeping-state-alive-for-a-specific-amount-of-time
Calling it with
ref.read
will result in the following output.Additional context
I understand that this behavior occurs when a Provider that is not
ref.watch
and notkeepAlive
exists across frames. For this reason, I would be happy to improve the documentation, not the implementation.example: https://github.com/koji-1009/riverpod_keepalive_example
The text was updated successfully, but these errors were encountered: