From f7688c5001e9081ebdc086f141ef294522cdeba5 Mon Sep 17 00:00:00 2001 From: chunhtai <47866232+chunhtai@users.noreply.github.com> Date: Mon, 13 Jul 2020 16:11:03 -0700 Subject: [PATCH] issue 61346 route can be added and disposed in the same frame (#61375) --- .../flutter/lib/src/widgets/navigator.dart | 6 +++- .../flutter/test/widgets/navigator_test.dart | 32 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/packages/flutter/lib/src/widgets/navigator.dart b/packages/flutter/lib/src/widgets/navigator.dart index 1e0ddb74e9..168099677e 100644 --- a/packages/flutter/lib/src/widgets/navigator.dart +++ b/packages/flutter/lib/src/widgets/navigator.dart @@ -213,7 +213,11 @@ abstract class Route { // focused child can only be attached to navigator after initState which // will be guarded by the asynchronous gap. TickerFuture.complete().then((void _) { - navigator.focusScopeNode.requestFocus(); + // The route can be disposed before the ticker future completes. This can + // happen when the navigator is under a TabView that does a warping. The + // navigator will be null in this case, and we have to do a null-safe + // operation. + navigator?.focusScopeNode?.requestFocus(); }); } diff --git a/packages/flutter/test/widgets/navigator_test.dart b/packages/flutter/test/widgets/navigator_test.dart index 2faa0cb1e6..1ab66a8baa 100644 --- a/packages/flutter/test/widgets/navigator_test.dart +++ b/packages/flutter/test/widgets/navigator_test.dart @@ -489,6 +489,38 @@ void main() { expect(observations[2].previous, '/A'); }); + testWidgets('Route didAdd and dispose in same frame work', (WidgetTester tester) async { + // Regression Test for https://github.com/flutter/flutter/issues/61346. + Widget buildNavigator() { + return Navigator( + pages: >[ + MaterialPage( + builder: (BuildContext context) => const Placeholder(), + ) + ], + onPopPage: (Route route, dynamic result) => false, + ); + } + final TabController controller = TabController(length: 3, vsync: tester); + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: TabBarView( + controller: controller, + children: [ + buildNavigator(), + buildNavigator(), + buildNavigator(), + ], + ) + ), + ); + + // This test should finish without crashing. + controller.index = 2; + await tester.pumpAndSettle(); + }); + testWidgets('replaceNamed replaces', (WidgetTester tester) async { final Map routes = { '/' : (BuildContext context) => OnTapPage(id: '/', onTap: () { Navigator.pushReplacementNamed(context, '/A'); }),