From ab1b865e58a69e7f6c2f3566532b5bbc838d678d Mon Sep 17 00:00:00 2001 From: hangyu Date: Wed, 13 Sep 2023 12:36:51 -0700 Subject: [PATCH] Dispose routes in navigator when throwing exception (#134596) fixes: #133695 ## 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, or this PR is [test-exempt]. - [ ] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. [Contributor Guide]: https://github.com/flutter/flutter/wiki/Tree-hygiene#overview [Tree Hygiene]: https://github.com/flutter/flutter/wiki/Tree-hygiene [test-exempt]: https://github.com/flutter/flutter/wiki/Tree-hygiene#tests [Flutter Style Guide]: https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo [Features we expect every widget to implement]: https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/wiki/Tree-hygiene#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/wiki/Chat --- packages/flutter/lib/src/widgets/navigator.dart | 3 +++ packages/flutter/test/material/app_test.dart | 10 +--------- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/packages/flutter/lib/src/widgets/navigator.dart b/packages/flutter/lib/src/widgets/navigator.dart index ccf3fbfb56..e2f350d299 100644 --- a/packages/flutter/lib/src/widgets/navigator.dart +++ b/packages/flutter/lib/src/widgets/navigator.dart @@ -2792,6 +2792,9 @@ class Navigator extends StatefulWidget { ); return true; }()); + for (final Route? route in result) { + route?.dispose(); + } result.clear(); } } else if (initialRouteName != Navigator.defaultRouteName) { diff --git a/packages/flutter/test/material/app_test.dart b/packages/flutter/test/material/app_test.dart index 16bbac3557..31d86997a8 100644 --- a/packages/flutter/test/material/app_test.dart +++ b/packages/flutter/test/material/app_test.dart @@ -334,15 +334,7 @@ void main() { expect(find.text('route "/a/b"'), findsNothing); expect(find.text('route "/b"'), findsNothing); } - }, - // TODO(polina-c): remove after fixing - // https://github.com/flutter/flutter/issues/133695 - leakTrackingTestConfig: const LeakTrackingTestConfig( - notDisposedAllowList: { - 'ValueNotifier': 3, - 'MaterialPageRoute': 3, - }, - )); + }); testWidgetsWithLeakTracking('Make sure initialRoute is only used the first time', (WidgetTester tester) async { final Map routes = {