Skip to content
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

fix SearchAnchor disposing SearchController while it is still used #155219

Merged
merged 34 commits into from
Sep 25, 2024

Conversation

PurplePolyhedron
Copy link
Contributor

@PurplePolyhedron PurplePolyhedron commented Sep 15, 2024

fixes #155180
New behaviour: SearchAnchor now closes the menu when itself is disposed while the menu is still open. This is the behaviour of MenuAnchor/OverlayPortal.

Pre-launch Checklist

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

@github-actions github-actions bot added framework flutter/packages/flutter repository. See also f: labels. f: material design flutter/packages/flutter/material repository. labels Sep 15, 2024
@PurplePolyhedron PurplePolyhedron marked this pull request as ready for review September 16, 2024 06:11
@PurplePolyhedron
Copy link
Contributor Author

I moved the disposal from _SearchViewRoute to _ViewContentState and added more comments.

Copy link
Member

@nate-thegrate nate-thegrate left a comment

Choose a reason for hiding this comment

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

I'm really impressed with how the search anchor has been updated to elegantly handle any combination of the search view route & search anchor being created/disposed.

My main concern is the concept of "spaghetti code": adding a few extra members isn't a huge deal but does make the API slightly more difficult to understand & maintain.


My current inclination is to wait a few days to see if anyone has ideas for improving readability; if not, I'd be happy to approve this 🙂

packages/flutter/lib/src/material/search_anchor.dart Outdated Show resolved Hide resolved
packages/flutter/test/material/search_anchor_test.dart Outdated Show resolved Hide resolved
packages/flutter/test/material/search_anchor_test.dart Outdated Show resolved Hide resolved
packages/flutter/test/material/search_anchor_test.dart Outdated Show resolved Hide resolved
packages/flutter/test/material/search_anchor_test.dart Outdated Show resolved Hide resolved
Copy link
Member

@nate-thegrate nate-thegrate 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 the updates!


I think I figured out a way to use 1 boolean flag instead of 2:

class SearchController {
  // If set to true, the SearchAnchor or ViewContent should
  // dispose of the controller when it's no longer in use.
  bool _internal = false;
}

class _SearchAnchorState {
  SearchController get _searchController {
    return widget.searchController
        ?? (_internalSearchController ??= SearchController().._internal = true);
  }

  void dispose() {
    widget.searchController?._detach(this);
    _internalSearchController?._detach(this);

    final _SearchViewRoute? route = _route;
    if (route != null && route.isActive) {
      route.navigator!.removeRoute(route);
    } else {
      _internalSearchController?.dispose();
    }
    super.dispose();
  }
}


class _ViewContentState {
  void dispose() {
    if (_controller._internal && !_controller.isAttached) {
      _controller.dispose();
    }
  }
}

Let me know what you think!

@PurplePolyhedron
Copy link
Contributor Author

PurplePolyhedron commented Sep 18, 2024

Thanks for the updates!

I think I figured out a way to use 1 boolean flag instead of 2:

class SearchController {
  // If set to true, the SearchAnchor or ViewContent should
  // dispose of the controller when it's no longer in use.
  bool _internal = false;
}

class _SearchAnchorState {
  SearchController get _searchController {
    return widget.searchController
        ?? (_internalSearchController ??= SearchController().._internal = true);
  }

  void dispose() {
    widget.searchController?._detach(this);
    _internalSearchController?._detach(this);

    final _SearchViewRoute? route = _route;
    if (route != null && route.isActive) {
      route.navigator!.removeRoute(route);
    } else {
      _internalSearchController?.dispose();
    }
    super.dispose();
  }
}


class _ViewContentState {
  void dispose() {
    if (_controller._internal && !_controller.isAttached) {
      _controller.dispose();
    }
  }
}

Let me know what you think!

Thanks for the suggestion, I tried it but it crashes using the example from the issue. My bad for not covering this in the test cases. I've now added a new test for that.
I think the crash is due to the route is considered not isActive during the exit animation.

However it did inspired me to come up with a new idea.

Copy link
Contributor

@QuncCccccc QuncCccccc 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 the fix! I haven't taken a closer look at this implementation but seems the sample code in the issue still throw the same exception:)

Copy link
Contributor

@QuncCccccc QuncCccccc left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks a lot for the fix!

@@ -3419,6 +3419,151 @@ void main() {
expect(find.byType(Placeholder), findsOneWidget);
});
});

testWidgets('SearchAnchor does not dispose external SeachController', (WidgetTester tester) async {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
testWidgets('SearchAnchor does not dispose external SeachController', (WidgetTester tester) async {
testWidgets('SearchAnchor does not dispose external SearchController', (WidgetTester tester) async {

@nate-thegrate nate-thegrate added the autosubmit Merge PR when tree becomes green via auto submit App label Sep 25, 2024
@auto-submit auto-submit bot merged commit 7677aac into flutter:master Sep 25, 2024
74 checks passed
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Sep 25, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Sep 25, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Sep 25, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Sep 25, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Sep 25, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Sep 25, 2024
auto-submit bot pushed a commit to flutter/packages that referenced this pull request Sep 25, 2024
Roll Flutter from 4ca51a1de597 to 538e7424d018 (40 revisions)

flutter/flutter@4ca51a1...538e742

2024-09-25 fluttergithubbot@gmail.com Marks Linux build_aar_module_test to be unflaky (flutter/flutter#155349)
2024-09-25 engine-flutter-autoroll@skia.org Roll Packages from 4926c0f to 7da2374 (3 revisions) (flutter/flutter#155701)
2024-09-25 98614782+auto-submit[bot]@users.noreply.github.com Reverts "Normalize TabBarTheme (#155476)" (flutter/flutter#155698)
2024-09-25 36861262+QuncCccccc@users.noreply.github.com Normalize TabBarTheme (flutter/flutter#155476)
2024-09-25 christopherfujino@gmail.com increase both linux & windows tool integration test shards (flutter/flutter#155631)
2024-09-25 engine-flutter-autoroll@skia.org Roll Flutter Engine from c7cd559e483b to d6d5fdba6ae1 (1 revision) (flutter/flutter#155693)
2024-09-25 sokolovskyi.konstantin@gmail.com Add WidgetStateBorderSide example and tests for it. (flutter/flutter#155559)
2024-09-25 engine-flutter-autoroll@skia.org Roll Flutter Engine from b9dd7a39dd58 to c7cd559e483b (1 revision) (flutter/flutter#155686)
2024-09-25 engine-flutter-autoroll@skia.org Roll Flutter Engine from 87c1667dfd1e to b9dd7a39dd58 (1 revision) (flutter/flutter#155684)
2024-09-25 engine-flutter-autoroll@skia.org Roll Flutter Engine from 05211f9d2267 to 87c1667dfd1e (1 revision) (flutter/flutter#155681)
2024-09-25 engine-flutter-autoroll@skia.org Roll Flutter Engine from 8d1eb7410b49 to 05211f9d2267 (1 revision) (flutter/flutter#155672)
2024-09-25 engine-flutter-autoroll@skia.org Roll Flutter Engine from ad3dd0df0fe7 to 8d1eb7410b49 (2 revisions) (flutter/flutter#155662)
2024-09-25 engine-flutter-autoroll@skia.org Roll Flutter Engine from 746ce6124844 to ad3dd0df0fe7 (2 revisions) (flutter/flutter#155653)
2024-09-25 magder@google.com Add PrivacyInfo.xcprivacy to macOS plugin template (flutter/flutter#155570)
2024-09-25 engine-flutter-autoroll@skia.org Roll Flutter Engine from 559f2ff31c74 to 746ce6124844 (14 revisions) (flutter/flutter#155648)
2024-09-25 120297255+PurplePolyhedron@users.noreply.github.com fix `SearchAnchor` disposing `SearchController` while it is still used (flutter/flutter#155219)
2024-09-25 137456488+flutter-pub-roller-bot@users.noreply.github.com Roll pub packages (flutter/flutter#155640)
2024-09-24 hhugh@google.com Preserve transform when using *Gradient:withOpacity (flutter/flutter#154908)
2024-09-24 nick9822@gmail.com fixed keyboardDismissBehavior on scroll without a drag (flutter/flutter#154675)
2024-09-24 engine-flutter-autoroll@skia.org Roll Flutter Engine from 7cd3d0b1bb2e to 559f2ff31c74 (3 revisions) (flutter/flutter#155629)
2024-09-24 engine-flutter-autoroll@skia.org Roll Flutter Engine from 2a13c3a27e1f to 7cd3d0b1bb2e (4 revisions) (flutter/flutter#155625)
2024-09-24 parlough@gmail.com Misc docs cleanup and fixes (flutter/flutter#155501)
2024-09-24 engine-flutter-autoroll@skia.org Roll Flutter Engine from dc44f95b7027 to 2a13c3a27e1f (1 revision) (flutter/flutter#155619)
2024-09-24 engine-flutter-autoroll@skia.org Roll Flutter Engine from 2745b8797025 to dc44f95b7027 (1 revision) (flutter/flutter#155616)
2024-09-24 engine-flutter-autoroll@skia.org Roll Flutter Engine from 8a54cc56d4b9 to 2745b8797025 (2 revisions) (flutter/flutter#155610)
2024-09-24 engine-flutter-autoroll@skia.org Roll Flutter Engine from c07812775255 to 8a54cc56d4b9 (2 revisions) (flutter/flutter#155607)
2024-09-24 engine-flutter-autoroll@skia.org Roll Packages from 9de72be to 4926c0f (4 revisions) (flutter/flutter#155605)
2024-09-24 danny@tuppeny.com Fix some broken links in DAP readme (flutter/flutter#155600)
2024-09-24 engine-flutter-autoroll@skia.org Roll Flutter Engine from 22e4f015cc99 to c07812775255 (2 revisions) (flutter/flutter#155599)
2024-09-24 danny@tuppeny.com [flutter_tools] Fix encoded stderr in "dart.log" from debug adapter to client (flutter/flutter#155249)
2024-09-24 engine-flutter-autoroll@skia.org Roll Flutter Engine from 309468cfd1bb to 22e4f015cc99 (2 revisions) (flutter/flutter#155591)
2024-09-24 dacoharkes@google.com [native assets] Roll dependencies (flutter/flutter#155432)
2024-09-24 engine-flutter-autoroll@skia.org Roll Flutter Engine from 4013dc28a48b to 309468cfd1bb (2 revisions) (flutter/flutter#155588)
2024-09-24 engine-flutter-autoroll@skia.org Roll Flutter Engine from 8a5af19a43f3 to 4013dc28a48b (1 revision) (flutter/flutter#155585)
2024-09-24 engine-flutter-autoroll@skia.org Roll Flutter Engine from 95c5a0940ad9 to 8a5af19a43f3 (10 revisions) (flutter/flutter#155583)
2024-09-24 sokolovskyi.konstantin@gmail.com Add `WidgetStateProperty` example and tests for it. (flutter/flutter#155315)
2024-09-24 30870216+gaaclarke@users.noreply.github.com Redo flutter engine flutter autoroll bd3d1990 485b 419c 8c55 b27e3eeb15ed 1727117767 (flutter/flutter#155579)
2024-09-23 engine-flutter-autoroll@skia.org Roll Flutter Engine from 61f0a3fbabbe to 9bb0ece79ae2 (2 revisions) (flutter/flutter#155549)
2024-09-23 magder@google.com Assert macOS framework artifact contains xcprivacy manifest (flutter/flutter#155189)
2024-09-23 engine-flutter-autoroll@skia.org Roll Packages from f54fe93 to 9de72be (1 revision) (flutter/flutter#155540)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-packages
Please CC louisehsu@google.com,stuartmorgan@google.com on the revert to ensure that a human
is aware of the problem.
...
thejitenpatel pushed a commit to thejitenpatel/flutter that referenced this pull request Sep 26, 2024
flutter#155219)

fixes flutter#155180
New behaviour: SearchAnchor now closes the menu when itself is disposed while the menu is still open. This is the behaviour of `MenuAnchor`/`OverlayPortal`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
autosubmit Merge PR when tree becomes green via auto submit App f: material design flutter/packages/flutter/material repository. framework flutter/packages/flutter repository. See also f: labels.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SearchAnchor tries to use its controller after being disposed
4 participants