Skip to content

Commit

Permalink
issue 61346 route can be added and disposed in the same frame (flutte…
Browse files Browse the repository at this point in the history
  • Loading branch information
chunhtai authored Jul 13, 2020
1 parent 82666ef commit f7688c5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/flutter/lib/src/widgets/navigator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,11 @@ abstract class Route<T> {
// focused child can only be attached to navigator after initState which
// will be guarded by the asynchronous gap.
TickerFuture.complete().then<void>((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();
});
}

Expand Down
32 changes: 32 additions & 0 deletions packages/flutter/test/widgets/navigator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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: <Page<void>>[
MaterialPage<void>(
builder: (BuildContext context) => const Placeholder(),
)
],
onPopPage: (Route<dynamic> route, dynamic result) => false,
);
}
final TabController controller = TabController(length: 3, vsync: tester);
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: TabBarView(
controller: controller,
children: <Widget>[
buildNavigator(),
buildNavigator(),
buildNavigator(),
],
)
),
);

// This test should finish without crashing.
controller.index = 2;
await tester.pumpAndSettle();
});

testWidgets('replaceNamed replaces', (WidgetTester tester) async {
final Map<String, WidgetBuilder> routes = <String, WidgetBuilder>{
'/' : (BuildContext context) => OnTapPage(id: '/', onTap: () { Navigator.pushReplacementNamed(context, '/A'); }),
Expand Down

0 comments on commit f7688c5

Please sign in to comment.