Unverified Commit de884f1a authored by Greg Spencer's avatar Greg Spencer Committed by GitHub

Remove the `nullOk` parameter from `Navigator.of` and add `Navigator.maybeOf` (#70726)

Adds Navigator.maybeOf to replace calling Navigator.of(context, nullOk: true), and removes the nullOk parameter. Also changes Navigator.of to return a non-nullable value, and removes many (120!) instances of the ! operator, reducing the possible places where a null dereference could occur.
parent 05dadb01
......@@ -117,7 +117,7 @@ class ExitButton extends StatelessWidget {
),
onPressed: () {
// The demo is on the root navigator.
Navigator.of(context, rootNavigator: true)!.pop();
Navigator.of(context, rootNavigator: true).pop();
},
);
}
......@@ -202,7 +202,7 @@ class Tab1RowItem extends StatelessWidget {
final Widget row = GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () {
Navigator.of(context)!.push(CupertinoPageRoute<void>(
Navigator.of(context).push(CupertinoPageRoute<void>(
title: colorName,
builder: (BuildContext context) => Tab1ItemPage(
color: color,
......@@ -752,7 +752,7 @@ class CupertinoDemoTab3 extends StatelessWidget {
const Padding(padding: EdgeInsets.only(top: 32.0)),
GestureDetector(
onTap: () {
Navigator.of(context, rootNavigator: true)!.push(
Navigator.of(context, rootNavigator: true).push(
CupertinoPageRoute<bool>(
fullscreenDialog: true,
builder: (BuildContext context) => Tab3Dialog(),
......@@ -800,7 +800,7 @@ class Tab3Dialog extends StatelessWidget {
child: const Text('Cancel'),
padding: EdgeInsets.zero,
onPressed: () {
Navigator.of(context)!.pop(false);
Navigator.of(context).pop(false);
},
),
),
......
......@@ -126,13 +126,13 @@ class FullScreenDialogDemoState extends State<FullScreenDialogDemo> {
TextButton(
child: const Text('CANCEL'),
onPressed: () {
Navigator.of(context)!.pop(false); // Pops the confirmation dialog but not the page.
Navigator.of(context).pop(false); // Pops the confirmation dialog but not the page.
},
),
TextButton(
child: const Text('DISCARD'),
onPressed: () {
Navigator.of(context)!.pop(true); // Returning true to _onWillPop will pop again.
Navigator.of(context).pop(true); // Returning true to _onWillPop will pop again.
},
),
],
......
......@@ -98,7 +98,7 @@ class _SearchDemoState extends State<SearchDemo> {
floatingActionButton: FloatingActionButton.extended(
tooltip: 'Back', // Tests depend on this label to exit the demo.
onPressed: () {
Navigator.of(context)!.pop();
Navigator.of(context).pop();
},
label: const Text('Close demo'),
icon: const Icon(Icons.close),
......
......@@ -151,11 +151,11 @@ class TextFormFieldDemoState extends State<TextFormFieldDemo> {
actions: <Widget> [
TextButton(
child: const Text('YES'),
onPressed: () { Navigator.of(context)!.pop(true); },
onPressed: () { Navigator.of(context).pop(true); },
),
TextButton(
child: const Text('NO'),
onPressed: () { Navigator.of(context)!.pop(false); },
onPressed: () { Navigator.of(context).pop(false); },
),
],
);
......
......@@ -35,7 +35,7 @@ class _LoginPageState extends State<LoginPage> {
// The login screen is immediately displayed on top of the Shrine
// home screen using onGenerateRoute and so rootNavigator must be
// set to true in order to get out of Shrine completely.
Navigator.of(context, rootNavigator: true)!.pop();
Navigator.of(context, rootNavigator: true).pop();
},
),
),
......@@ -95,7 +95,7 @@ class _LoginPageState extends State<LoginPage> {
// the Shrine home screen using onGenerateRoute and so
// rootNavigator must be set to true in order to get out
// of Shrine completely.
Navigator.of(context, rootNavigator: true)!.pop();
Navigator.of(context, rootNavigator: true).pop();
},
child: const Text('CANCEL'),
),
......
......@@ -107,7 +107,7 @@ class _TransformationsDemoState extends State<TransformationsDemo> {
TextButton(
child: const Text('OK'),
onPressed: () {
Navigator.of(context)!.pop();
Navigator.of(context).pop();
},
),
],
......
......@@ -70,7 +70,7 @@ class VideoCard extends StatelessWidget {
});
controller!.setVolume(1.0);
Navigator.of(context)!.push(route);
Navigator.of(context).push(route);
}
return SafeArea(
......
......@@ -292,7 +292,7 @@ class _CupertinoContextMenuState extends State<CupertinoContextMenu> with Ticker
return widget.previewBuilder!(context, animation, widget.child);
},
);
Navigator.of(context, rootNavigator: true)!.push<void>(_route!);
Navigator.of(context, rootNavigator: true).push<void>(_route!);
_route!.animation!.addStatusListener(_routeAnimationStatusListener);
}
......@@ -686,7 +686,7 @@ class _ContextMenuRoute<T> extends PopupRoute<T> {
parent: animation!,
curve: const Interval(0.9, 1.0),
));
Navigator.of(context)!.pop();
Navigator.of(context).pop();
}
// Take measurements on the child and _ContextMenuSheet and update the
......
......@@ -1060,7 +1060,7 @@ Future<T?> showCupertinoModalPopup<T>({
RouteSettings? routeSettings,
}) {
assert(useRootNavigator != null);
return Navigator.of(context, rootNavigator: useRootNavigator)!.push(
return Navigator.of(context, rootNavigator: useRootNavigator).push(
_CupertinoModalPopupRoute<T>(
barrierColor: CupertinoDynamicColor.resolve(barrierColor, context),
barrierDismissible: barrierDismissible,
......
......@@ -292,7 +292,7 @@ void showLicensePage({
}) {
assert(context != null);
assert(useRootNavigator != null);
Navigator.of(context, rootNavigator: useRootNavigator)!.push(MaterialPageRoute<void>(
Navigator.of(context, rootNavigator: useRootNavigator).push(MaterialPageRoute<void>(
builder: (BuildContext context) => LicensePage(
applicationName: applicationName,
applicationVersion: applicationVersion,
......@@ -1342,8 +1342,8 @@ class _MasterDetailFlowState extends State<_MasterDetailFlow> implements _PageOp
? widget.masterPageBuilder!(c, false)
: _MasterPage(
leading: widget.leading ??
(widget.automaticallyImplyLeading && Navigator.of(context)!.canPop()
? BackButton(onPressed: () => Navigator.of(context)!.pop())
(widget.automaticallyImplyLeading && Navigator.of(context).canPop()
? BackButton(onPressed: () => Navigator.of(context).pop())
: null),
title: widget.title,
centerTitle: widget.centerTitle,
......@@ -1364,7 +1364,7 @@ class _MasterDetailFlowState extends State<_MasterDetailFlow> implements _PageOp
onWillPop: () async {
// No need for setState() as rebuild happens on navigation pop.
focus = _Focus.master;
Navigator.of(context)!.pop();
Navigator.of(context).pop();
return false;
},
child: BlockSemantics(child: widget.detailPageBuilder(context, arguments, null)),
......
......@@ -669,7 +669,7 @@ Future<T?> showModalBottomSheet<T>({
assert(debugCheckHasMediaQuery(context));
assert(debugCheckHasMaterialLocalizations(context));
final NavigatorState navigator = Navigator.of(context, rootNavigator: useRootNavigator)!;
final NavigatorState navigator = Navigator.of(context, rootNavigator: useRootNavigator);
return navigator.push(_ModalBottomSheetRoute<T>(
builder: builder,
capturedThemes: InheritedTheme.capture(from: context, to: navigator.context),
......
......@@ -978,7 +978,7 @@ Future<T?> showDialog<T>({
assert(useRootNavigator != null);
assert(debugCheckHasMaterialLocalizations(context));
final CapturedThemes themes = InheritedTheme.capture(from: context, to: Navigator.of(context, rootNavigator: useRootNavigator)!.context);
final CapturedThemes themes = InheritedTheme.capture(from: context, to: Navigator.of(context, rootNavigator: useRootNavigator).context);
return showGeneralDialog(
context: context,
pageBuilder: (BuildContext buildContext, Animation<double> animation, Animation<double> secondaryAnimation) {
......
......@@ -1206,7 +1206,7 @@ class _DropdownButtonState<T> extends State<DropdownButton<T>> with WidgetsBindi
)
];
final NavigatorState navigator = Navigator.of(context)!;
final NavigatorState navigator = Navigator.of(context);
assert(_dropdownRoute == null);
_dropdownRoute = _DropdownRoute<T>(
items: menuItems,
......
......@@ -842,7 +842,7 @@ Future<T?> showMenu<T>({
semanticLabel ??= MaterialLocalizations.of(context).popupMenuLabel;
}
final NavigatorState navigator = Navigator.of(context, rootNavigator: useRootNavigator)!;
final NavigatorState navigator = Navigator.of(context, rootNavigator: useRootNavigator);
return navigator.push(_PopupMenuRoute<T>(
position: position,
items: items,
......@@ -1060,7 +1060,7 @@ class PopupMenuButtonState<T> extends State<PopupMenuButton<T>> {
void showButtonMenu() {
final PopupMenuThemeData popupMenuTheme = PopupMenuTheme.of(context);
final RenderBox button = context.findRenderObject()! as RenderBox;
final RenderBox overlay = Navigator.of(context)!.overlay!.context.findRenderObject()! as RenderBox;
final RenderBox overlay = Navigator.of(context).overlay!.context.findRenderObject()! as RenderBox;
final RelativeRect position = RelativeRect.fromRect(
Rect.fromPoints(
button.localToGlobal(widget.offset, ancestor: overlay),
......
......@@ -60,7 +60,7 @@ Future<T?> showSearch<T>({
assert(context != null);
delegate.query = query ?? delegate.query;
delegate._currentBody = _SearchBody.suggestions;
return Navigator.of(context)!.push(_SearchPageRoute<T>(
return Navigator.of(context).push(_SearchPageRoute<T>(
delegate: delegate,
));
}
......@@ -294,7 +294,7 @@ abstract class SearchDelegate<T> {
void close(BuildContext context, T result) {
_currentBody = null;
_focusNode?.unfocus();
Navigator.of(context)!
Navigator.of(context)
..popUntil((Route<dynamic> route) => route == _route)
..pop(result);
}
......
......@@ -664,7 +664,7 @@ class _DismissModalAction extends DismissAction {
@override
Object invoke(DismissIntent intent) {
return Navigator.of(context)!.maybePop();
return Navigator.of(context).maybePop();
}
}
......@@ -1849,7 +1849,7 @@ Future<T?> showGeneralDialog<T extends Object?>({
assert(pageBuilder != null);
assert(useRootNavigator != null);
assert(!barrierDismissible || barrierLabel != null);
return Navigator.of(context, rootNavigator: useRootNavigator)!.push<T>(_DialogRoute<T>(
return Navigator.of(context, rootNavigator: useRootNavigator).push<T>(_DialogRoute<T>(
pageBuilder: pageBuilder,
barrierDismissible: barrierDismissible,
barrierLabel: barrierLabel,
......
......@@ -1652,7 +1652,7 @@ void main() {
return RaisedButton(
child: const Text('Home'),
onPressed: () {
navigator = Navigator.of(context)!;
navigator = Navigator.of(context);
assert(navigator != null);
navigator.push<void>(r);
},
......
......@@ -322,7 +322,7 @@ void main() {
child: CupertinoButton(
child: const Text('Next'),
onPressed: () {
Navigator.of(context)!.push(
Navigator.of(context).push(
CupertinoPageRoute<void>(
builder: (BuildContext context) {
return CupertinoPageScaffold(
......@@ -333,7 +333,7 @@ void main() {
child: CupertinoButton(
child: const Text('Back'),
onPressed: () {
Navigator.of(context)!.pop();
Navigator.of(context).pop();
},
),
),
......
......@@ -40,7 +40,7 @@ void main() {
return CupertinoButton(
child: const Text('go to second page'),
onPressed: () {
Navigator.of(context)!.pushNamed('/2');
Navigator.of(context).pushNamed('/2');
},
);
},
......@@ -131,7 +131,7 @@ void main() {
return CupertinoButton(
child: const Text('go to second page'),
onPressed: () {
Navigator.of(context)!.pushNamed('/2');
Navigator.of(context).pushNamed('/2');
},
);
},
......@@ -156,7 +156,7 @@ void main() {
return CupertinoButton(
child: const Text('go to second page'),
onPressed: () {
Navigator.of(context)!.pushNamed('/2');
Navigator.of(context).pushNamed('/2');
},
);
},
......@@ -245,7 +245,7 @@ void main() {
builder: (BuildContext context) => CupertinoButton(
child: const Text('home'),
onPressed: () {
Navigator.of(context)!.restorablePushNamed('/2');
Navigator.of(context).restorablePushNamed('/2');
},
),
routes: <String, WidgetBuilder>{
......@@ -269,7 +269,7 @@ void main() {
expect(find.text('home'), findsNothing);
expect(find.text('second route'), findsOneWidget);
Navigator.of(tester.element(find.text('second route')))!.pop();
Navigator.of(tester.element(find.text('second route'))).pop();
await tester.pumpAndSettle();
expect(find.text('home'), findsOneWidget);
......@@ -280,7 +280,7 @@ void main() {
expect(find.text('home'), findsNothing);
expect(find.text('second route'), findsOneWidget);
Navigator.of(tester.element(find.text('second route')))!.pop();
Navigator.of(tester.element(find.text('second route'))).pop();
await tester.pumpAndSettle();
expect(find.text('home'), findsOneWidget);
......
......@@ -100,7 +100,7 @@ void main() {
return Material(
child: ElevatedButton(
child: const Text('X'),
onPressed: () { Navigator.of(context)!.pushNamed('/next'); },
onPressed: () { Navigator.of(context).pushNamed('/next'); },
),
);
}
......@@ -258,7 +258,7 @@ void main() {
child: ElevatedButton(
child: const Text('X'),
onPressed: () async {
result = Navigator.of(context)!.pushNamed<Object?>('/a');
result = Navigator.of(context).pushNamed<Object?>('/a');
},
),
);
......@@ -270,7 +270,7 @@ void main() {
child: ElevatedButton(
child: const Text('Y'),
onPressed: () {
Navigator.of(context)!.pop('all done');
Navigator.of(context).pop('all done');
},
),
);
......
......@@ -1813,7 +1813,7 @@ void main() {
SimpleDialogOption(
child: const Text('X'),
onPressed: () {
Navigator.of(context)!.pop();
Navigator.of(context).pop();
},
),
],
......
......@@ -41,7 +41,7 @@ void main() {
ElevatedButton(
child: const Text('push unwrapped'),
onPressed: () {
Navigator.of(context)!.push<void>(
Navigator.of(context).push<void>(
MaterialPageRoute<void>(
// The primaryBox will see the default Theme when built.
builder: (BuildContext _) => primaryBox,
......@@ -52,7 +52,7 @@ void main() {
ElevatedButton(
child: const Text('push wrapped'),
onPressed: () {
Navigator.of(context)!.push<void>(
Navigator.of(context).push<void>(
MaterialPageRoute<void>(
// Capture the shadow Theme.
builder: (BuildContext _) => InheritedTheme.captureAll(context, primaryBox),
......@@ -84,7 +84,7 @@ void main() {
await tester.pumpAndSettle(); // route animation
expect(containerColor(), primaryColor);
Navigator.of(navigatorContext)!.pop();
Navigator.of(navigatorContext).pop();
await tester.pumpAndSettle(); // route animation
// Show the route which contains primaryBox
......@@ -181,7 +181,7 @@ void main() {
ElevatedButton(
child: const Text('push unwrapped'),
onPressed: () {
Navigator.of(context)!.push<void>(
Navigator.of(context).push<void>(
MaterialPageRoute<void>(
// The Banner will see the default BannerTheme when built.
builder: (BuildContext _) => banner,
......@@ -192,7 +192,7 @@ void main() {
ElevatedButton(
child: const Text('push wrapped'),
onPressed: () {
Navigator.of(context)!.push<void>(
Navigator.of(context).push<void>(
MaterialPageRoute<void>(
// Capture the shadow BannerTheme.
builder: (BuildContext _) => InheritedTheme.captureAll(context, banner),
......@@ -234,7 +234,7 @@ void main() {
expect(getTextStyle('hello').fontSize, bannerFontSize);
expect(getTextStyle('hello').color, bannerTextColor);
Navigator.of(navigatorContext)!.pop();
Navigator.of(navigatorContext).pop();
await tester.pumpAndSettle(); // route animation
await tester.tap(find.text('push unwrapped'));
......@@ -271,7 +271,7 @@ void main() {
ElevatedButton(
child: const Text('push unwrapped'),
onPressed: () {
Navigator.of(context)!.push<void>(
Navigator.of(context).push<void>(
MaterialPageRoute<void>(
// The Banner will see the default BannerTheme when built.
builder: (BuildContext _) => divider,
......@@ -282,7 +282,7 @@ void main() {
ElevatedButton(
child: const Text('push wrapped'),
onPressed: () {
Navigator.of(context)!.push<void>(
Navigator.of(context).push<void>(
MaterialPageRoute<void>(
// Capture the shadow BannerTheme.
builder: (BuildContext _) => InheritedTheme.captureAll(context, divider),
......@@ -316,7 +316,7 @@ void main() {
expect(dividerBorder().color, dividerColor);
expect(dividerBorder().width, dividerThickness);
Navigator.of(navigatorContext)!.pop();
Navigator.of(navigatorContext).pop();
await tester.pumpAndSettle(); // route animation
await tester.tap(find.text('push unwrapped'));
......@@ -375,7 +375,7 @@ void main() {
ElevatedButton(
child: const Text('push unwrapped'),
onPressed: () {
Navigator.of(context)!.push<void>(
Navigator.of(context).push<void>(
MaterialPageRoute<void>(
// The Banner will see the default BannerTheme when built.
builder: (BuildContext _) => listTiles,
......@@ -386,7 +386,7 @@ void main() {
ElevatedButton(
child: const Text('push wrapped'),
onPressed: () {
Navigator.of(context)!.push<void>(
Navigator.of(context).push<void>(
MaterialPageRoute<void>(
// Capture the shadow BannerTheme.
builder: (BuildContext _) => InheritedTheme.captureAll(context, listTiles),
......@@ -429,7 +429,7 @@ void main() {
expect(getIconStyle(selectedIconKey).color, tileSelectedColor);
expect(getIconStyle(unselectedIconKey).color, tileIconColor);
Navigator.of(navigatorContext)!.pop();
Navigator.of(navigatorContext).pop();
await tester.pumpAndSettle(); // route animation
await tester.tap(find.text('push unwrapped'));
......@@ -475,7 +475,7 @@ void main() {
ElevatedButton(
child: const Text('push unwrapped'),
onPressed: () {
Navigator.of(context)!.push<void>(
Navigator.of(context).push<void>(
MaterialPageRoute<void>(
// The slider will see the default SliderTheme when built.
builder: (BuildContext _) => slider,
......@@ -486,7 +486,7 @@ void main() {
ElevatedButton(
child: const Text('push wrapped'),
onPressed: () {
Navigator.of(context)!.push<void>(
Navigator.of(context).push<void>(
MaterialPageRoute<void>(
// Capture the shadow SliderTheme.
builder: (BuildContext _) => InheritedTheme.captureAll(context, slider),
......@@ -513,7 +513,7 @@ void main() {
expect(sliderBox, paints..rrect(color: activeTrackColor)..rrect(color: inactiveTrackColor));
expect(sliderBox, paints..circle(color: thumbColor));
Navigator.of(navigatorContext)!.pop();
Navigator.of(navigatorContext).pop();
await tester.pumpAndSettle(); // route animation
await tester.tap(find.text('push unwrapped'));
......@@ -560,7 +560,7 @@ void main() {
ElevatedButton(
child: const Text('push unwrapped'),
onPressed: () {
Navigator.of(context)!.push<void>(
Navigator.of(context).push<void>(
MaterialPageRoute<void>(
// The slider will see the default ToggleButtonsTheme when built.
builder: (BuildContext _) => toggleButtons,
......@@ -571,7 +571,7 @@ void main() {
ElevatedButton(
child: const Text('push wrapped'),
onPressed: () {
Navigator.of(context)!.push<void>(
Navigator.of(context).push<void>(
MaterialPageRoute<void>(
// Capture the shadow toggleButtons.
builder: (BuildContext _) => InheritedTheme.captureAll(context, toggleButtons),
......@@ -603,7 +603,7 @@ void main() {
expect(getTextColor('selected'), selectedButtonColor);
expect(getTextColor('unselected'), buttonColor);
Navigator.of(navigatorContext)!.pop();
Navigator.of(navigatorContext).pop();
await tester.pumpAndSettle(); // route animation
await tester.tap(find.text('push unwrapped'));
......@@ -649,7 +649,7 @@ void main() {
RaisedButton(
child: const Text('push unwrapped'),
onPressed: () {
Navigator.of(context)!.push<void>(
Navigator.of(context).push<void>(
MaterialPageRoute<void>(
// The slider will see the default ButtonTheme when built.
builder: (BuildContext _) => buttons,
......@@ -660,7 +660,7 @@ void main() {
RaisedButton(
child: const Text('push wrapped'),
onPressed: () {
Navigator.of(context)!.push<void>(
Navigator.of(context).push<void>(
MaterialPageRoute<void>(
// Capture the shadow toggleButtons.
builder: (BuildContext _) => InheritedTheme.captureAll(context, buttons),
......@@ -695,7 +695,7 @@ void main() {
expect(getButtonColor('disabled'), disabledButtonColor);
expect(getButtonColor('enabled'), buttonColor);
Navigator.of(navigatorContext)!.pop();
Navigator.of(navigatorContext).pop();
await tester.pumpAndSettle(); // route animation
await tester.tap(find.text('push unwrapped'));
......
......@@ -282,7 +282,7 @@ void main() {
'/': (BuildContext context) => Material(
child: TextButton(
child: const Text('PUSH'),
onPressed: () { Navigator.of(context)!.pushNamed('/b'); },
onPressed: () { Navigator.of(context).pushNamed('/b'); },
),
),
'/b': (BuildContext context) => Container(child: const Text('HELLO')),
......@@ -737,7 +737,7 @@ void main() {
expect(homeTapCount, 1);
expect(pageTapCount, 0);
final ValueNotifier<bool> notifier = Navigator.of(homeScaffoldKey.currentContext!)!.userGestureInProgressNotifier;
final ValueNotifier<bool> notifier = Navigator.of(homeScaffoldKey.currentContext!).userGestureInProgressNotifier;
expect(notifier.value, false);
Navigator.push<void>(homeScaffoldKey.currentContext!, MaterialPageRoute<void>(
......
......@@ -26,7 +26,7 @@ void main() {
'/': (BuildContext context) => Material(
child: TextButton(
child: const Text('push'),
onPressed: () { Navigator.of(context)!.pushNamed('/b'); },
onPressed: () { Navigator.of(context).pushNamed('/b'); },
),
),
'/b': (BuildContext context) => const Text('page b'),
......@@ -52,7 +52,7 @@ void main() {
'/': (BuildContext context) => Material(
child: TextButton(
child: const Text('push'),
onPressed: () { Navigator.of(context)!.pushNamed('/b'); },
onPressed: () { Navigator.of(context).pushNamed('/b'); },
),
),
'/b': (BuildContext context) => const Text('page b'),
......@@ -85,7 +85,7 @@ void main() {
'/': (BuildContext context) => Material(
child: TextButton(
child: const Text('push'),
onPressed: () { Navigator.of(context)!.pushNamed('/b'); },
onPressed: () { Navigator.of(context).pushNamed('/b'); },
),
),
'/b': (BuildContext context) => const Text('page b'),
......@@ -125,7 +125,7 @@ void main() {
'/': (BuildContext context) => Material(
child: TextButton(
child: const Text('push'),
onPressed: () { Navigator.of(context)!.pushNamed('/b'); },
onPressed: () { Navigator.of(context).pushNamed('/b'); },
),
),
'/b': (BuildContext context) => const Text('page b'),
......@@ -169,7 +169,7 @@ void main() {
'/': (BuildContext context) => Material(
child: TextButton(
child: const Text('push'),
onPressed: () { Navigator.of(context)!.pushNamed('/b'); },
onPressed: () { Navigator.of(context).pushNamed('/b'); },
),
),
'/b': (BuildContext context) => StatefulBuilder(
......
......@@ -124,7 +124,7 @@ void main() {
await tester.tap(find.byKey(withCallbackKey));
await tester.pump();
await tester.pump(const Duration(seconds: 1));
Navigator.of(popupContext)!.pop();
Navigator.of(popupContext).pop();
await tester.pump();
expect(cancels, equals(2));
});
......
......@@ -1372,12 +1372,12 @@ void main() {
ElevatedButton(
child: const Text('Next'),
onPressed: () {
Navigator.of(context)!.pushReplacement(
Navigator.of(context).pushReplacement(
MaterialPageRoute<void>(
builder: (BuildContext context) {
return ElevatedButton(
child: const Text('Inner page'),
onPressed: () { Navigator.of(context)!.pop(); },
onPressed: () { Navigator.of(context).pop(); },
);
},
),
......
......@@ -2086,12 +2086,12 @@ void main() {
ElevatedButton(
child: const Text('Next'),
onPressed: () {
Navigator.of(context)!.pushReplacement(
Navigator.of(context).pushReplacement(
MaterialPageRoute<void>(
builder: (BuildContext context) {
return ElevatedButton(
child: const Text('Inner page'),
onPressed: () { Navigator.of(context)!.pop(); },
onPressed: () { Navigator.of(context).pop(); },
);
},
),
......
......@@ -2035,7 +2035,7 @@ void main() {
key: transitionTarget,
child: const Text('PUSH'),
onPressed: () {
Navigator.of(context)!.pushNamed('/second');
Navigator.of(context).pushNamed('/second');
},
),
),
......
......@@ -38,7 +38,7 @@ void main() {
expect(tester.testTextInput.isVisible, isFalse);
Navigator.of(tester.element(find.text('Dialog')))!.pop();
Navigator.of(tester.element(find.text('Dialog'))).pop();
await tester.pump();
expect(focusNode.hasPrimaryFocus, isTrue);
......
......@@ -139,7 +139,7 @@ void main() {
child: TextButton(
child: const Text('X'),
onPressed: () {
Navigator.of(context)!.push(MaterialPageRoute<void>(
Navigator.of(context).push(MaterialPageRoute<void>(
builder: (BuildContext context) {
return SampleForm(
callback: () => Future<bool>.value(willPopValue),
......@@ -178,7 +178,7 @@ void main() {
child: TextButton(
child: const Text('X'),
onPressed: () {
Navigator.of(context)!.push(MaterialPageRoute<void>(
Navigator.of(context).push(MaterialPageRoute<void>(
builder: (BuildContext context) {
return SampleForm(
callback: () => Future<bool>.value(willPopValue),
......@@ -230,11 +230,11 @@ void main() {
actions: <Widget> [
TextButton(
child: const Text('YES'),
onPressed: () { Navigator.of(context)!.pop(true); },
onPressed: () { Navigator.of(context).pop(true); },
),
TextButton(
child: const Text('NO'),
onPressed: () { Navigator.of(context)!.pop(false); },
onPressed: () { Navigator.of(context).pop(false); },
),
],
);
......@@ -252,7 +252,7 @@ void main() {
child: TextButton(
child: const Text('X'),
onPressed: () {
Navigator.of(context)!.push(MaterialPageRoute<void>(
Navigator.of(context).push(MaterialPageRoute<void>(
builder: (BuildContext context) {
return SampleForm(
callback: () => showYesNoAlert(context),
......@@ -338,7 +338,7 @@ void main() {
child: TextButton(
child: const Text('X'),
onPressed: () {
Navigator.of(context)!.push(route);
Navigator.of(context).push(route);
},
),
);
......
......@@ -349,7 +349,7 @@ void main() {
focusNode: testNode1,
autofocus: true,
onPressed: () {
Navigator.of(context)!.push<void>(
Navigator.of(context).push<void>(
MaterialPageRoute<void>(
builder: (BuildContext context) {
return Center(
......@@ -358,7 +358,7 @@ void main() {
focusNode: testNode2,
autofocus: true,
onPressed: () {
Navigator.of(context)!.pop();
Navigator.of(context).pop();
},
child: const Text('Go Back'),
),
......@@ -1148,7 +1148,7 @@ void main() {
focusNode: testNode1,
autofocus: true,
onPressed: () {
Navigator.of(context)!.push<void>(
Navigator.of(context).push<void>(
MaterialPageRoute<void>(
builder: (BuildContext context) {
return Center(
......@@ -1159,7 +1159,7 @@ void main() {
focusNode: testNode2,
autofocus: true,
onPressed: () {
Navigator.of(context)!.pop();
Navigator.of(context).pop();
},
child: const Text('Go Back'),
),
......
......@@ -59,7 +59,7 @@ void main() {
behavior: HitTestBehavior.opaque,
onTap: () {
navigatorContext = context;
Navigator.of(context)!.push(
Navigator.of(context).push(
TestRoute(
useCaptureAll
? InheritedTheme.captureAll(context, const IconTextBox('Hello'))
......@@ -121,7 +121,7 @@ void main() {
// Return to the home route
useCaptureAll = true;
Navigator.of(navigatorContext)!.pop();
Navigator.of(navigatorContext).pop();
await tester.pumpAndSettle(); // route transition
// Verify that all is the same as it was when the test started
......
......@@ -204,13 +204,13 @@ void main() {
return ElevatedButton(
child: const Text('Next'),
onPressed: () {
Navigator.of(context)!.push(
Navigator.of(context).push(
MaterialPageRoute<void>(
builder: (BuildContext context) {
return ElevatedButton(
child: const Text('Inner page'),
onPressed: () {
Navigator.of(context, rootNavigator: true)!.push(
Navigator.of(context, rootNavigator: true).push(
MaterialPageRoute<void>(
builder: (BuildContext context) {
return const Text('Dialog');
......@@ -783,8 +783,8 @@ void main() {
'/A/B': (BuildContext context) => OnTapPage(
id: 'B',
onTap: (){
Navigator.of(context)!.popUntil((Route<dynamic> route) => route.isFirst);
Navigator.of(context)!.pushReplacementNamed('/C');
Navigator.of(context).popUntil((Route<dynamic> route) => route.isFirst);
Navigator.of(context).pushReplacementNamed('/C');
},
),
'/C': (BuildContext context) => const OnTapPage(id: 'C',
......@@ -851,7 +851,7 @@ void main() {
id: 'B',
onTap: (){
// Pops all routes with bad predicate.
Navigator.of(context)!.popUntil((Route<dynamic> route) => false);
Navigator.of(context).popUntil((Route<dynamic> route) => false);
},
),
};
......@@ -929,7 +929,7 @@ void main() {
'/A/B': (BuildContext context) => OnTapPage(
id: 'B',
onTap: () {
Navigator.of(context)!.pushNamedAndRemoveUntil('/D', ModalRoute.withName('/A'));
Navigator.of(context).pushNamedAndRemoveUntil('/D', ModalRoute.withName('/A'));
},
),
'/D': (BuildContext context) => const Text('page D'),
......@@ -1597,7 +1597,7 @@ void main() {
final Map<String, WidgetBuilder> routes = <String, WidgetBuilder>{
'/' : (BuildContext context) => OnTapPage(id: '/', onTap: () {
Navigator.pushNamed(context, '/A');
Navigator.of(context)!.pop();
Navigator.of(context).pop();
}),
'/A': (BuildContext context) => OnTapPage(id: 'A', onTap: () { Navigator.pop(context); }),
};
......@@ -1825,12 +1825,12 @@ void main() {
});
testWidgets('initial routes below opaque route are offstage', (WidgetTester tester) async {
final GlobalKey<NavigatorState> g = GlobalKey<NavigatorState>();
final GlobalKey<NavigatorState> testKey = GlobalKey<NavigatorState>();
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: Navigator(
key: g,
key: testKey,
initialRoute: '/a/b',
onGenerateRoute: (RouteSettings s) {
return MaterialPageRoute<void>(
......@@ -1850,7 +1850,7 @@ void main() {
expect(find.text('+/a+', skipOffstage: false), findsOneWidget);
expect(find.text('+/a/b+'), findsOneWidget);
g.currentState!.pop();
testKey.currentState!.pop();
await tester.pumpAndSettle();
expect(find.text('+/+'), findsNothing);
......@@ -1858,7 +1858,7 @@ void main() {
expect(find.text('+/a+'), findsOneWidget);
expect(find.text('+/a/b+'), findsNothing);
g.currentState!.pop();
testKey.currentState!.pop();
await tester.pumpAndSettle();
expect(find.text('+/+'), findsOneWidget);
......@@ -1868,12 +1868,12 @@ void main() {
testWidgets('Can provide custom onGenerateInitialRoutes', (WidgetTester tester) async {
bool onGenerateInitialRoutesCalled = false;
final GlobalKey<NavigatorState> g = GlobalKey<NavigatorState>();
final GlobalKey<NavigatorState> testKey = GlobalKey<NavigatorState>();
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: Navigator(
key: g,
key: testKey,
initialRoute: 'Hello World',
onGenerateInitialRoutes: (NavigatorState navigator, String initialRoute) {
onGenerateInitialRoutesCalled = true;
......@@ -1893,7 +1893,7 @@ void main() {
expect(find.text('Hello'), findsNothing);
expect(find.text('World'), findsOneWidget);
g.currentState!.pop();
testKey.currentState!.pop();
await tester.pumpAndSettle();
expect(find.text('Hello'), findsOneWidget);
......@@ -1901,16 +1901,16 @@ void main() {
});
testWidgets('Navigator.of able to handle input context is a navigator context', (WidgetTester tester) async {
final GlobalKey<NavigatorState> g = GlobalKey<NavigatorState>();
final GlobalKey<NavigatorState> testKey = GlobalKey<NavigatorState>();
await tester.pumpWidget(
MaterialApp(
navigatorKey: g,
navigatorKey: testKey,
home: const Text('home'),
)
);
final NavigatorState state = Navigator.of(g.currentContext!)!;
expect(state, g.currentState);
final NavigatorState state = Navigator.of(testKey.currentContext!);
expect(state, testKey.currentState);
});
testWidgets('Navigator.of able to handle input context is a navigator context - root navigator', (WidgetTester tester) async {
......@@ -1931,7 +1931,61 @@ void main() {
)
);
final NavigatorState state = Navigator.of(sub.currentContext!, rootNavigator: true)!;
final NavigatorState state = Navigator.of(sub.currentContext!, rootNavigator: true);
expect(state, root.currentState);
});
testWidgets('Navigator.maybeOf throws when there is no navigator', (WidgetTester tester) async {
final GlobalKey<NavigatorState> testKey = GlobalKey<NavigatorState>();
await tester.pumpWidget(SizedBox(key: testKey));
expect(() async {
Navigator.of(testKey.currentContext!);
}, throwsFlutterError);
});
testWidgets('Navigator.maybeOf works when there is no navigator', (WidgetTester tester) async {
final GlobalKey<NavigatorState> testKey = GlobalKey<NavigatorState>();
await tester.pumpWidget(SizedBox(key: testKey));
final NavigatorState? state = Navigator.maybeOf(testKey.currentContext!);
expect(state, isNull);
});
testWidgets('Navigator.maybeOf able to handle input context is a navigator context', (WidgetTester tester) async {
final GlobalKey<NavigatorState> testKey = GlobalKey<NavigatorState>();
await tester.pumpWidget(
MaterialApp(
navigatorKey: testKey,
home: const Text('home'),
)
);
final NavigatorState? state = Navigator.maybeOf(testKey.currentContext!);
expect(state, isNotNull);
expect(state, testKey.currentState);
});
testWidgets('Navigator.maybeOf able to handle input context is a navigator context - root navigator', (WidgetTester tester) async {
final GlobalKey<NavigatorState> root = GlobalKey<NavigatorState>();
final GlobalKey<NavigatorState> sub = GlobalKey<NavigatorState>();
await tester.pumpWidget(
MaterialApp(
navigatorKey: root,
home: Navigator(
key: sub,
onGenerateRoute: (RouteSettings settings) {
return MaterialPageRoute<void>(
settings: settings,
builder: (BuildContext context) => const Text('dummy'),
);
},
),
)
);
final NavigatorState? state = Navigator.maybeOf(sub.currentContext!, rootNavigator: true);
expect(state, isNotNull);
expect(state, root.currentState);
});
......
......@@ -26,7 +26,7 @@ class HomePage extends StatefulWidget {
class _HomePageState extends State<HomePage> {
void _presentModalPage() {
Navigator.of(context)!.push(PageRouteBuilder<void>(
Navigator.of(context).push(PageRouteBuilder<void>(
transitionDuration: const Duration(milliseconds: 300),
barrierColor: Colors.black54,
opaque: false,
......@@ -62,7 +62,7 @@ class ModalPage extends StatelessWidget {
highlightColor: Colors.transparent,
splashColor: Colors.transparent,
onTap: () {
Navigator.of(context)!.pop();
Navigator.of(context).pop();
},
child: const SizedBox.expand(),
),
......
......@@ -563,7 +563,7 @@ void main() {
builder: (BuildContext context) {
return ElevatedButton(
onPressed: () {
Navigator.of(context)!.push<void>(
Navigator.of(context).push<void>(
PageRouteBuilder<void>(
settings: settings,
pageBuilder: (BuildContext context, Animation<double> input, Animation<double> out) {
......@@ -612,7 +612,7 @@ void main() {
builder: (BuildContext context) {
return ElevatedButton(
onPressed: () {
Navigator.of(context)!.push<void>(
Navigator.of(context).push<void>(
PageRouteBuilder<void>(
settings: settings,
pageBuilder: (BuildContext context, Animation<double> input, Animation<double> out) {
......@@ -1138,7 +1138,7 @@ void main() {
builder: (BuildContext context) {
return ElevatedButton(
onPressed: () {
Navigator.of(context)!.push<void>(
Navigator.of(context).push<void>(
MaterialPageRoute<void>(
builder: (BuildContext innerContext) {
return Container(
......@@ -1188,7 +1188,7 @@ void main() {
builder: (BuildContext context) {
return ElevatedButton(
onPressed: () {
Navigator.of(context)!.push<void>(
Navigator.of(context).push<void>(
ModifiedReverseTransitionDurationRoute<void>(
builder: (BuildContext innerContext) {
return Container(
......@@ -1247,7 +1247,7 @@ void main() {
builder: (BuildContext context) {
return ElevatedButton(
onPressed: () {
Navigator.of(context)!.push<void>(
Navigator.of(context).push<void>(
ModifiedReverseTransitionDurationRoute<void>(
builder: (BuildContext innerContext) {
return Container(
......@@ -1309,7 +1309,7 @@ void main() {
child: ElevatedButton(
child: const Text('X'),
onPressed: () {
Navigator.of(context)!.push<void>(
Navigator.of(context).push<void>(
_TestDialogRouteWithCustomBarrierCurve<void>(
child: const Text('Hello World'),
)
......@@ -1371,7 +1371,7 @@ void main() {
child: ElevatedButton(
child: const Text('X'),
onPressed: () {
Navigator.of(context)!.push<void>(
Navigator.of(context).push<void>(
_TestDialogRouteWithCustomBarrierCurve<void>(
child: const Text('Hello World'),
barrierCurve: Curves.linear,
......@@ -1436,7 +1436,7 @@ void main() {
child: ElevatedButton(
child: const Text('X'),
onPressed: () {
Navigator.of(context)!.push<void>(
Navigator.of(context).push<void>(
_TestDialogRouteWithCustomBarrierCurve<void>(
child: const Text('Hello World'),
barrierLabel: 'test label',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment