从图库/照相机中取消图像选择器时出现Image.path错误

问题描述 投票:0回答:1

我具有使用“图库”或“相机”选项编辑图像的功能。我不知道当用户尝试从图库中挑选要切换到相机时按下后退按钮时该如何处理。

调试器随后显示“在Null上调用Image.path的错误”;

flutter dart flutter-layout flutter-image
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/

© www.soinside.com 2019 - 2024. All rights reserved.