【问题标题】:Image.path Error when Cancelling Image Picker from Gallery/Camera从图库/相机中取消图像选择器时出现 Image.path 错误
【发布时间】:2020-01-29 11:50:10
【问题描述】:

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

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

解决方案

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

【问题讨论】:

  • 请向我们展示您的脚本

标签: flutter dart flutter-layout flutter-image


【解决方案1】:

你可以用 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-04-17
    • 2013-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多