Replies: 3 comments
-
|
I have the same question. |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
Are you looking for |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
The maintainer's hint is correct you're looking for The Difference
Solution for Your Use Caseclass MyWidget extends ConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
// Use select (not selectAsync) for widgets
final firstNameAsync = ref.watch(
userProvider.select((asyncValue) => asyncValue.whenData((user) => user.firstName)),
);
return firstNameAsync.when(
data: (firstName) => Text('Hello $firstName'),
loading: () => const CircularProgressIndicator(),
error: (error, stack) => Text('Error: $error'),
);
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Question: Why does
selectAsyncreturnFutureinstead ofAsyncValue?Context
I'm reading the documentation about performance optimization using
selectAsyncfor asynchronous providers. The documentation shows this example:Question
I understand that returning a
Futureworks well within async provider functions where we can useawait. However, I'm confused about how to useselectAsyncin widget build methods, which are synchronous contexts.Why does
selectAsyncreturnFuture<T>instead ofAsyncValue<T>?Use Case
In a widget build method, I want to:
Current Problem
Expected Behavior
I would expect something like:
Current Workaround and Issues
Option 1: Skip selectAsync (loses performance benefit)
Problem: This defeats the purpose of
selectAsyncfor performance optimization, as the widget will rebuild when any property ofuserchanges, not justfirstName.Option 2: Use selectAsync + useFuture (inconsistent API)
Problem: This creates API inconsistency in the codebase:
AsyncValuewith.when(),.maybeWhen(), etc.AsyncSnapshotwith.connectionState,.hasError, etc.Questions
selectAsyncin widget build methods?selectAsyncreturnAsyncValue<T>instead ofFuture<T>?selectAsyncValue) for widget contexts?AsyncValueandAsyncSnapshotin a large codebase? Having some widgets use.when()pattern and others use.connectionStatemakes the code harder to maintain.Impact on Developer Experience
The current design forces developers to choose between:
selectAsync+useFuturebut with inconsistent APIs)AsyncValueeverywhere but losing performance benefits)This shouldn't be a trade-off we have to make.
Thanks for any guidance on this!
Beta Was this translation helpful? Give feedback.
All reactions