Skip to content

Commit c18aa80

Browse files
Fix Navigator.pushNamed<T>() type checking (flutter#188148)
This PR allows the navigator's `pushNamed()` method to be called with any type argument without immediately throwing an error. It also brings the framework more in line with the [style guide](https://github.com/flutter/flutter/blob/master/docs/contributing/Style-guide-for-Flutter-repo.md#prefer-explicit-types-and-avoid-dynamic): > Avoid `dynamic`. If the type is unknown, prefer using `Object` (or `Object?`) and casting <br> Fixes flutter#57186
1 parent 76fbda5 commit c18aa80

2 files changed

Lines changed: 87 additions & 21 deletions

File tree

‎packages/flutter/lib/src/widgets/navigator.dart‎

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3028,11 +3028,7 @@ class Navigator extends StatefulWidget {
30283028
return true;
30293029
}());
30303030
result.add(
3031-
navigator._routeNamed<dynamic>(
3032-
Navigator.defaultRouteName,
3033-
arguments: null,
3034-
allowNull: true,
3035-
),
3031+
navigator._routeNamed(Navigator.defaultRouteName, arguments: null, allowNull: true),
30363032
);
30373033
final List<String> routeParts = initialRouteName.split('/');
30383034
if (initialRouteName.isNotEmpty) {
@@ -3043,7 +3039,7 @@ class Navigator extends StatefulWidget {
30433039
debugRouteNames!.add(routeName);
30443040
return true;
30453041
}());
3046-
result.add(navigator._routeNamed<dynamic>(routeName, arguments: null, allowNull: true));
3042+
result.add(navigator._routeNamed(routeName, arguments: null, allowNull: true));
30473043
}
30483044
}
30493045
if (result.last == null) {
@@ -3067,9 +3063,7 @@ class Navigator extends StatefulWidget {
30673063
} else if (initialRouteName != Navigator.defaultRouteName) {
30683064
// If initialRouteName wasn't '/', then we try to get it with allowNull:true, so that if that fails,
30693065
// we fall back to '/' (without allowNull:true, see below).
3070-
result.add(
3071-
navigator._routeNamed<dynamic>(initialRouteName, arguments: null, allowNull: true),
3072-
);
3066+
result.add(navigator._routeNamed(initialRouteName, arguments: null, allowNull: true));
30733067
}
30743068
// Null route might be a result of gap in initialRouteName
30753069
//
@@ -3079,7 +3073,7 @@ class Navigator extends StatefulWidget {
30793073
// result = ['A', 'A/B/C'].
30803074
result.removeWhere((Route<dynamic>? route) => route == null);
30813075
if (result.isEmpty) {
3082-
result.add(navigator._routeNamed<dynamic>(Navigator.defaultRouteName, arguments: null));
3076+
result.add(navigator._routeNamed(Navigator.defaultRouteName, arguments: null));
30833077
}
30843078
return result.cast<Route<dynamic>>();
30853079
}
@@ -4685,7 +4679,7 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res
46854679
return index < _history.length ? _history[index] : null;
46864680
}
46874681

4688-
Route<T?>? _routeNamed<T>(String name, {required Object? arguments, bool allowNull = false}) {
4682+
Route<Object?>? _routeNamed(String name, {required Object? arguments, bool allowNull = false}) {
46894683
assert(!_debugLocked);
46904684
if (allowNull && widget.onGenerateRoute == null) {
46914685
return null;
@@ -4704,7 +4698,7 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res
47044698
return true;
47054699
}());
47064700
final settings = RouteSettings(name: name, arguments: arguments);
4707-
var route = widget.onGenerateRoute!(settings) as Route<T?>?;
4701+
Route<Object?>? route = widget.onGenerateRoute!(settings);
47084702
if (route == null && !allowNull) {
47094703
assert(() {
47104704
if (widget.onUnknownRoute == null) {
@@ -4725,7 +4719,7 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res
47254719
}
47264720
return true;
47274721
}());
4728-
route = widget.onUnknownRoute!(settings) as Route<T?>?;
4722+
route = widget.onUnknownRoute!(settings);
47294723
assert(() {
47304724
if (route == null) {
47314725
throw FlutterError.fromParts(<DiagnosticsNode>[
@@ -4773,7 +4767,8 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res
47734767
@awaitNotRequired
47744768
@optionalTypeArgs
47754769
Future<T?> pushNamed<T extends Object?>(String routeName, {Object? arguments}) {
4776-
return push<T?>(_routeNamed<T>(routeName, arguments: arguments)!);
4770+
final Route<Object?> route = _routeNamed(routeName, arguments: arguments)!;
4771+
return push<Object?>(route).then((Object? result) => result as T?);
47774772
}
47784773

47794774
/// Push a named route onto the navigator.
@@ -4843,10 +4838,10 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res
48434838
TO? result,
48444839
Object? arguments,
48454840
}) {
4846-
return pushReplacement<T?, TO>(
4847-
_routeNamed<T>(routeName, arguments: arguments)!,
4841+
return pushReplacement<Object?, Object?>(
4842+
_routeNamed(routeName, arguments: arguments)!,
48484843
result: result,
4849-
);
4844+
).then((Object? value) => value as T?);
48504845
}
48514846

48524847
/// Replace the current route of the navigator by pushing the route named
@@ -4987,7 +4982,10 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res
49874982
RoutePredicate predicate, {
49884983
Object? arguments,
49894984
}) {
4990-
return pushAndRemoveUntil<T?>(_routeNamed<T>(newRouteName, arguments: arguments)!, predicate);
4985+
return pushAndRemoveUntil<Object?>(
4986+
_routeNamed(newRouteName, arguments: arguments)!,
4987+
predicate,
4988+
).then((Object? result) => result as T?);
49914989
}
49924990

49934991
/// Push the route with the given name onto the navigator, and then remove all
@@ -6084,8 +6082,7 @@ class _NamedRestorationInformation extends _RestorationInformation {
60846082

60856083
@override
60866084
Route<dynamic> createRoute(NavigatorState navigator) {
6087-
final Route<dynamic> route = navigator._routeNamed<dynamic>(name, arguments: arguments)!;
6088-
return route;
6085+
return navigator._routeNamed(name, arguments: arguments)!;
60896086
}
60906087
}
60916088

‎packages/flutter/test/widgets/navigator_test.dart‎

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,14 +544,83 @@ void main() {
544544
'/': (BuildContext context) => const Text('/'),
545545
'/second': (BuildContext context) => const Text('/second'),
546546
};
547-
await tester.pumpWidget(MaterialApp(navigatorKey: nav, routes: routes));
547+
await tester.pumpWidget(TestWidgetsApp(navigatorKey: nav, routes: routes));
548548
expect(find.text('/'), findsOneWidget);
549549
nav.currentState!.pushNamed<Object>('/second');
550550
await tester.pumpAndSettle();
551551
expect(find.text('/'), findsNothing);
552552
expect(find.text('/second'), findsOneWidget);
553553
});
554554

555+
testWidgets('pushNamed can handle subtype of Object as type argument', (
556+
WidgetTester tester,
557+
) async {
558+
final routes = <String, WidgetBuilder>{
559+
'/': (BuildContext context) => const Text('/'),
560+
'/second': (BuildContext context) => const Text('/second'),
561+
};
562+
563+
await tester.pumpWidget(TestWidgetsApp(routes: routes));
564+
expect(find.text('/'), findsOneWidget);
565+
566+
final NavigatorState navigator = Navigator.of(tester.element(find.text('/')));
567+
final Future<bool?> result = navigator.pushNamed<bool>('/second');
568+
await tester.pumpAndSettle();
569+
expect(find.text('/'), findsNothing);
570+
expect(find.text('/second'), findsOneWidget);
571+
572+
navigator.pop<bool>(true);
573+
await tester.pumpAndSettle();
574+
expect(await result, isTrue);
575+
});
576+
577+
testWidgets('pushReplacementNamed can handle subtype of Object as type argument', (
578+
WidgetTester tester,
579+
) async {
580+
final routes = <String, WidgetBuilder>{
581+
'/': (BuildContext context) => const Text('/'),
582+
'/second': (BuildContext context) => const Text('/second'),
583+
};
584+
585+
await tester.pumpWidget(TestWidgetsApp(routes: routes));
586+
expect(find.text('/'), findsOneWidget);
587+
588+
final NavigatorState navigator = Navigator.of(tester.element(find.text('/')));
589+
final Future<bool?> result = navigator.pushReplacementNamed<bool, void>('/second');
590+
await tester.pumpAndSettle();
591+
expect(find.text('/'), findsNothing);
592+
expect(find.text('/second'), findsOneWidget);
593+
594+
navigator.pop<bool>(true);
595+
await tester.pumpAndSettle();
596+
expect(await result, isTrue);
597+
});
598+
599+
testWidgets('pushNamedAndRemoveUntil can handle subtype of Object as type argument', (
600+
WidgetTester tester,
601+
) async {
602+
final routes = <String, WidgetBuilder>{
603+
'/': (BuildContext context) => const Text('/'),
604+
'/second': (BuildContext context) => const Text('/second'),
605+
};
606+
607+
await tester.pumpWidget(TestWidgetsApp(routes: routes));
608+
expect(find.text('/'), findsOneWidget);
609+
610+
final NavigatorState navigator = Navigator.of(tester.element(find.text('/')));
611+
final Future<bool?> result = navigator.pushNamedAndRemoveUntil<bool>(
612+
'/second',
613+
(Route<dynamic> route) => false,
614+
);
615+
await tester.pumpAndSettle();
616+
expect(find.text('/'), findsNothing);
617+
expect(find.text('/second'), findsOneWidget);
618+
619+
navigator.pop<bool>(true);
620+
await tester.pumpAndSettle();
621+
expect(await result, isTrue);
622+
});
623+
555624
testWidgets('Pending gestures are rejected', (WidgetTester tester) async {
556625
final log = <String>[];
557626
final routes = <String, WidgetBuilder>{

0 commit comments

Comments
 (0)