Flutter 为什么我在异步函数中使用setState而不是更新状态(flatter)?

Flutter 为什么我在异步函数中使用setState而不是更新状态(flatter)?,flutter,dart,setstate,Flutter,Dart,Setstate,为什么我的图像不更新? 当用户从多媒体资料或照相机中选择新图像时,我需要更新图像属性,但当用户选择新图像时,我使用setState将新值设置为图像属性,但它不起作用(未更新用户界面) 这里是我的代码,我有问题: 很抱歉,我不能完整的源代码,因为stackoverflow的编辑器抱怨很多代码 全州级别: enum ChooseMedia{camera,gallery} File _image; void getImage(ChooseMedia media) async { if

为什么我的图像不更新?

当用户从多媒体资料或照相机中选择新图像时,我需要更新图像属性,但当用户选择新图像时,我使用setState将新值设置为图像属性,但它不起作用(未更新用户界面)

这里是我的代码,我有问题: 很抱歉,我不能完整的源代码,因为stackoverflow的编辑器抱怨很多代码

全州级别: enum ChooseMedia{camera,gallery}

  File _image;

  void getImage(ChooseMedia media) async {
    if (media == ChooseMedia.camera) {
      var image = await ImagePicker.pickImage(source: ImageSource.camera);
      var pathLocal = await getApplicationDocumentsDirectory();
      await image.copy('${pathLocal.path}/profileImage.jpg').then((_) {
        setState(() {
          _image = image;
        });
      });
    }
    if (ChooseMedia.gallery == media) {
      var image = await ImagePicker.pickImage(source: ImageSource.gallery);
      var pathLocal = await getApplicationDocumentsDirectory();

      await image.copy('${pathLocal.path}/profileImage.jpg').then((_) {
        setState(() {
          _image = image;
        });
      });
    }
  }



  @override
  Widget build(BuildContext context) {

        return PopupMenuButton(
          elevation: 5,
          shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.all(Radius.circular(15))),
          onSelected: (ChooseMedia media) {
            getImage(media);
          },
          itemBuilder: (BuildContext context) => <PopupMenuEntry<ChooseMedia>>[
            const PopupMenuItem<ChooseMedia>(
              value: ChooseMedia.gallery,
              child: ListTile(
                leading: Icon(
                  Icons.picture_in_picture,
                ),
                title: Text('From galaxy'),
              ),
            ),
            const PopupMenuItem<ChooseMedia>(
              value: ChooseMedia.camera,
              child: ListTile(
                leading: Icon(Icons.camera),
                title: Text('Take picture'),
              ),
            ),
          ],
          child: Container(
              decoration: BoxDecoration(),
              padding: EdgeInsets.all(7),
              child: CircleAvatar(
                  backgroundImage: _image == null
                      ? AssetImage('assets/images/profile.jpg')
                      : FileImage(_image))),
        );
  }

File\u图像;
void getImage(选择媒体)异步{
如果(媒体==选择媒体照相机){
var image=等待ImagePicker.pickImage(源:ImageSource.camera);
var pathLocal=await getApplicationDocumentsDirectory();
等待image.copy('${pathLocal.path}/profileImage.jpg')。然后(){
设置状态(){
_图像=图像;
});
});
}
如果(选择媒体库==媒体){
var image=wait ImagePicker.pickImage(源:ImageSource.gallery);
var pathLocal=await getApplicationDocumentsDirectory();
等待image.copy('${pathLocal.path}/profileImage.jpg')。然后(){
设置状态(){
_图像=图像;
});
});
}
}
@凌驾
小部件构建(构建上下文){
返回弹出菜单按钮(
标高:5,
形状:圆形矩形边框(
borderRadius:borderRadius.all(半径.圆形(15)),
当选:(选择媒体){
getImage(媒体);
},
itemBuilder:(构建上下文)=>[
常数PopupMenuItem(
值:ChooseMedia.gallery,
孩子:ListTile(
领先:图标(
Icons.picture\u在\u图片中,
),
标题:文本(“来自银河”),
),
),
常数PopupMenuItem(
值:选择media.camera,
孩子:ListTile(
前导:图标(图标、摄像头),
标题:文本(“拍照”),
),
),
],
子:容器(
装饰:BoxDecoration(),
填充:边缘设置。全部(7),
孩子:圆环星(
背景图像:_image==null
?资产估值(‘资产/图像/档案.jpg’)
:FileImage(_image)),
);
}

您正在混合“等待”和“然后” 照办


您的文件是否被复制到您提供的位置?控制台日志怎么说?我像你一样使用保存,但它不工作,它只工作第一次和第二次选择图像它不更新:
var image=wait ImagePicker.pickImage(来源:ImageSource.gallery);var pathLocal=await getApplicationDocumentsDirectory();var saveFile=await image.copy('${pathLocal.path}/profileImage.jpg');setState((){image=saveFile;})这是因为映像路径始终相同,请执行类似于以下var pathLocal=wait getApplicationDocumentsDirectory()的操作;如果({u image!=null){u image.deleteSync();}setState((){u image=null;});var image=等待ImagePicker.pickImage(源:ImageSource.camera);var copiedImage=await image.copy('${pathLocal.path}/${DateTime.now()}.jpg');setState((){u image=copiedImage;});
var copiedImage = await image.copy('${pathLocal.path}/profileImage.jpg');
setState(() {
    _image = copiedImage;
});