Skip to content

Conversation

@sun-jiao
Copy link
Contributor

@sun-jiao sun-jiao commented May 17, 2023

Make suggestionsBuilder in SearchAnchor asyncable. 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

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read and followed the Flutter Style Guide, including Features we expect every widget to implement.
  • I signed the CLA.
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making.
  • All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel on Discord.

@flutter-dashboard flutter-dashboard bot added f: material design flutter/packages/flutter/material repository. framework flutter/packages/flutter repository. See also f: labels. labels May 17, 2023
@github-actions github-actions bot added d: api docs Issues with https://api.flutter.dev/ d: examples Sample code and demos documentation labels May 17, 2023
@sun-jiao
Copy link
Contributor Author

I think it will not be a breaking change if using FutureOr.

@flutter-dashboard
Copy link

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.

@sun-jiao sun-jiao changed the title make suggestionsBuilder in SearchAnchor async make suggestionsBuilder in SearchAnchor asyncable May 17, 2023
@sun-jiao
Copy link
Contributor Author

I'm not sure if new test cases are needed.

@HansMuller HansMuller requested a review from QuncCccccc May 17, 2023 20:25
Copy link
Contributor

@justinmc justinmc left a 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

Comment on lines 696 to 698
suggestionsFutureOr.then((Iterable<Widget> value) {
result = value;
});
Copy link
Contributor

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);
Copy link
Contributor

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);

Copy link
Contributor Author

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.

Copy link
Contributor

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'),
    );
  }
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also change the function type to Future<void> didChangeDependencies? I tried it locally and the linter shows a warning about its type. This works for me😀:
Screenshot 2023-05-17 at 3 50 22 PM

Copy link
Contributor Author

@sun-jiao sun-jiao May 17, 2023

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.

@sun-jiao
Copy link
Contributor Author

sun-jiao commented May 18, 2023

Hi, @justinmc , I'm trying to implement similar examples. But I'm now confused by your example 3, line 62

If this line is executed after line 55 of second query, but before line 61, I think the second query will also be teriminated here.

@flutter-dashboard
Copy link

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 package:flutter.

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.

Changes reported for pull request #127019 at sha 7d02714

@flutter-dashboard flutter-dashboard bot added the will affect goldens Changes to golden files label May 18, 2023
@sun-jiao sun-jiao requested a review from justinmc May 19, 2023 00:40
Copy link
Contributor

@justinmc justinmc left a 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);
Copy link
Contributor

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?

Copy link
Contributor Author

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().

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call 👍

@QuncCccccc QuncCccccc merged commit 7b67aa5 into flutter:master May 24, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 25, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 25, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 25, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 25, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 25, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 25, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 25, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 25, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 26, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 26, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Aug 16, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Aug 17, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Aug 17, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Aug 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

d: api docs Issues with https://api.flutter.dev/ d: examples Sample code and demos f: material design flutter/packages/flutter/material repository. will affect goldens Changes to golden files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants