2

我有一个功能,其中我使用图库或相机选项编辑我的图像。当用户在尝试从图库中选择切换到相机时按下后退按钮时,我不知道如何处理。

然后调试器显示“Image.path was called on Null”的错误;

解决方案

对于那些将遇到此错误的人。在图库或相机上点击返回。只是if (imageFile != null)用来避免应用程序崩溃。

4

1 回答 1

0

你可以用 WillPopScope 包裹你的脚手架

return  WillPopScope(
        onWillPop: () => _exitApp(context),
        child:  Scaffold(
            appBar:  AppBar(
              title:  Text("Navigation Demo"),
              backgroundColor: Colors.deepOrangeAccent,
            ),

并询问用户您要退出此应用程序还是当前页面?

Future<bool> _exitApp(BuildContext context) {
  return showDialog(
        context: context,
        child: new AlertDialog(
          title: new Text('Do you want to exit this application?'),
          content: new Text('We hate to see you leave...'),
          actions: <Widget>[
            new FlatButton(
              onPressed: () => Navigator.of(context).pop(false),
              child: new Text('No'),
            ),
            new FlatButton(
              onPressed: () => Navigator.of(context).pop(true),
              child: new Text('Yes'),
            ),
          ],
        ),
      ) ??
      false;
}

详细参考https://codingwithjoe.com/flutter-navigation-how-to-prevent-navigation/

于 2019-09-30T07:57:54.067 回答