首页 > 解决方案 > 在 Flutter 中关闭相机后出现白屏/黑屏

问题描述

我有一个小部件可以在我的应用程序上拍照,如下所示:

final File imageFile =
        await ImagePicker.pickImage(source: ImageSource.camera).then((test) {
      print('TEST $test');
      return test;
    });

我可以毫无错误地打开相机并拍照,但是当我尝试返回或接受我拍摄的照片时,应用程序会显示白屏,并且控制台根本没有显示任何错误。

这在真实设备(小米红米 Note 8t)上失败,但它适用于 Android 模拟器。

我能看到的唯一信息是Lost connection to device.当我拿起相机时

标签: flutterflutter-dependencies

解决方案


修复了添加 try catch:

Future<Null> _pickImageFromCamera(BuildContext context, int index) async {
    File imageFile;
    try {
      imageFile = await ImagePicker.pickImage(source: ImageSource.camera)
      .then((picture) {
        return picture; // I found this .then necessary
      });
    } catch (eror) {
      print('error taking picture ${error.toString()}');
    }
    setState(() => this._imageFile = imageFile);
  }

推荐阅读