-
Notifications
You must be signed in to change notification settings - Fork 29.7k
make suggestionsBuilder in SearchAnchor asyncable
#127019
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
Conversation
|
I think it will not be a breaking change if using |
|
It looks like this pull request may not have tests. Please make sure to add tests before merging. If you need an exemption to this rule, contact Hixie on the #hackers channel in Chat (don't just cc him here, he won't see it! He's on Discord!). If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix? Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing. |
suggestionsBuilder in SearchAnchor asyncsuggestionsBuilder in SearchAnchor asyncable
|
I'm not sure if new test cases are needed. |
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.
Thanks for doing this! I think you're right that we should be able to do this in an async fashion, it seems like a very common use case. And good call using FutureOr.
This reminds me of AutocompleteOptionsBuilder.
Could you actually add an example to the docs of how to use this with a fake "backend"? I just recently added the same kind of example for Autocomplete because people were confused by it: https://github.com/flutter/flutter/pull/126283/files
| suggestionsFutureOr.then((Iterable<Widget> value) { | ||
| result = value; | ||
| }); |
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.
What happens if suggestionsFutureOr throws an error?
| void didChangeDependencies() { | ||
| super.didChangeDependencies(); | ||
| result = widget.suggestionsBuilder(context, _controller); | ||
| final FutureOr<Iterable<Widget>> suggestionsFutureOr = widget.suggestionsBuilder(context, _controller); |
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.
Can you make this async and then do this?
result = await widget.suggestionsBuilder(context, _controller);
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.
Is it right? didChangeDependencies() overrides a sync function.
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.
I think it works? The code below compiles for me. Or any reason not to?
class Foo extends StatefulWidget {
const Foo({
super.key,
});
@override
State<Foo> createState() => _FooState();
}
class _FooState extends State<Foo> {
@override
void didUpdateWidget(Foo oldWidget) async {
super.didUpdateWidget(oldWidget);
await Future.delayed(const Duration(seconds: 1));
print('delayed hello');
}
@override
Widget build(BuildContext context) {
return Container(
child: const Text('Foo'),
);
}
}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 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, I didn't realize that it could be placed on the last line to avoid using context across async.
I'm trying if (!mounted) {return;} locally.
|
Golden file changes have been found for this pull request. Click here to view and triage (e.g. because this is an intentional change). If you are still iterating on this change and are not ready to resolve the images on the Flutter Gold dashboard, consider marking this PR as a draft pull request above. You will still be able to view image results on the dashboard, commenting will be silenced, and the check will not try to resolve itself until marked ready for review. For more guidance, visit Writing a golden file test for Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing. |
justinmc
left a comment
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.
LGTM with nits 👍
You were right about the Autocomplete example! I opened a PR #127219. Thanks for pointing that out.
| _viewRect = Offset.zero & _screenSize!; | ||
| } | ||
| } | ||
| result = await widget.suggestionsBuilder(context, _controller); |
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.
Does this need to be wrapped in a setState since it happens asynchronously?
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.
I think it is equivalent to calling updateSuggestions().
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.
Good call 👍

Make
suggestionsBuilderinSearchAnchorasyncable. Because the search results usually come from an async API, for example, a http request.issue:
#126531
If you had to change anything in the flutter/tests repo, include a link to the migration guide as per the breaking change policy.(Deleted as it is no longer a breaking change.)Pre-launch Checklist
///).If you need help, consider asking for advice on the #hackers-new channel on Discord.