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.
This commit is contained in:
Greg Spencer
2020-11-20 09:10:46 -08:00
committed by GitHub
parent 05dadb0120
commit de884f1afe
36 changed files with 243 additions and 149 deletions

View File

@@ -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);
},
),
),

View File

@@ -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.
},
),
],

View File

@@ -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),

View File

@@ -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); },
),
],
);

View File

@@ -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'),
),

View File

@@ -107,7 +107,7 @@ class _TransformationsDemoState extends State<TransformationsDemo> {
TextButton(
child: const Text('OK'),
onPressed: () {
Navigator.of(context)!.pop();
Navigator.of(context).pop();
},
),
],

View File

@@ -70,7 +70,7 @@ class VideoCard extends StatelessWidget {
});
controller!.setVolume(1.0);
Navigator.of(context)!.push(route);
Navigator.of(context).push(route);
}
return SafeArea(