-
-
Notifications
You must be signed in to change notification settings - Fork 527
Add dispose to FutureProvider #912
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -35,7 +35,7 @@ class DeferredInheritedProvider<T, R> extends InheritedProvider<R> { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| DeferredInheritedProvider({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| Key? key, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| required Create<T> create, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| Dispose<T>? dispose, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| Dispose<R>? dispose, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| required DeferredStartListening<T, R> startListening, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| UpdateShouldNotify<R>? updateShouldNotify, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| bool? lazy, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -165,7 +165,7 @@ class _CreateDeferredInheritedProvider<T, R> extends _DeferredDelegate<T, R> { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| }) : super(updateShouldNotify, startListening); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| final Create<T> create; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| final Dispose<T>? dispose; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| final Dispose<R>? dispose; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| @override | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| _CreateDeferredInheritedProviderElement<T, R> createState() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -220,7 +220,13 @@ class _CreateDeferredInheritedProviderElement<T, R> | |||||||||||||||||||||||||||||||||||||||||||||||||||
| void dispose() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| super.dispose(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (_didBuild) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| delegate.dispose?.call(element!, _controller as T); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (T is Future<R>) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| (_controller as Future<R>).then((value) => delegate.dispose?.call(element!, value)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else if (isLoaded) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| delegate.dispose?.call(element!, value); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| delegate.dispose?.call(element!, _controller as R); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
221
to
+229
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Compile-time error & wrong cast inside
Suggested fix: - if (T is Future<R>) {
- (_controller as Future<R>).then((value) => delegate.dispose?.call(element!, value));
+ if (_controller is Future<R>) {
+ // FutureProvider – dispose after the Future completes.
+ (_controller as Future<R>).then(
+ (resolved) => delegate.dispose?.call(element!, resolved),
+ onError: (_) {}, // ignore errors, we cannot dispose a failed value
+ );
} else if (isLoaded) {
delegate.dispose?.call(element!, value);
} else {
- delegate.dispose?.call(element!, _controller as R);
+ // No value produced; fall back to disposing the controller itself.
+ // Cast to `dynamic` to avoid a bad runtime cast when T ≠ R.
+ delegate.dispose?.call(element!, _controller as dynamic);
}This eliminates the compile error and prevents a potential 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is impractical because more often than not, it's not
Tthat needs disposing when using futures/streams. It's something else, like a Completer/StreamController/...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, the problem I was running into was that if it were a
Futurethen I didn't have access to the data since it was already completed. How would I get around this to work forCompleteror aStreamController?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no easy solution with the API that Provider offers. We'd have to make a breaking change and do:
That's the API Riverpod uses.