Flutter:选择后删除选择文件图像
Flutter : delete pick file image after picked it
我想在选择图像后将其删除,但在执行此操作后显示此错误以及图像容器 return 错误屏幕
'package:flutter/src/painting/image_provider.dart': Failed assertion:
line 854 pos 14: 'file != null': is not true.
这是我用来删除图像和显示图像的一般方法
Container(
height: MediaQuery.of(context).size.height * 0.2, //list height
child: ListView.builder(
itemCount: uploadedFilesList.length,
shrinkWrap: true,
scrollDirection: Axis.horizontal,
itemBuilder: (context, index) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
child: Column(
children: [
InkWell(
onTap: () {
setState(() {
uploadedFilesList[index] = null;
});
},
child: Icon(Icons.close)),
Image.file(
uploadedFilesList[index],
width: MediaQuery.of(context).size.width * .25,
height: MediaQuery.of(context).size.height * .2,
),
],
),
),
);
},
),
),
谁能告诉我我的代码哪里出了问题?
在您的 setState 中,您正在设置 uploadedFilesList[index] = null;这意味着您尝试使用以下方式显示的列表中存在空值:
Image.file(uploadedFilesList[index], ....)
基本上,错误是说您将 null 传递给图像提供者。
要修复它,我认为您应该使用以下方法删除该索引处的元素:
setState(() => uploadedFilesList.removeAt(index));
而不是将其设置为空。
setState(() {
uploadedFilesList[index] = null;
});
那不应该为空
用任何虚拟图像替换 null 或用空容器替换图像提供程序。
您不能为图像提供程序提供空值。
uploadedFilesList[index]==null?Image.file(
uploadedFilesList[index],
width: MediaQuery.of(context).size.width * .25,
height: MediaQuery.of(context).size.height * .2,
):Container(
width: MediaQuery.of(context).size.width * .25,
height: MediaQuery.of(context).size.height * .2,),
我想在选择图像后将其删除,但在执行此操作后显示此错误以及图像容器 return 错误屏幕
'package:flutter/src/painting/image_provider.dart': Failed assertion: line 854 pos 14: 'file != null': is not true.
这是我用来删除图像和显示图像的一般方法
Container(
height: MediaQuery.of(context).size.height * 0.2, //list height
child: ListView.builder(
itemCount: uploadedFilesList.length,
shrinkWrap: true,
scrollDirection: Axis.horizontal,
itemBuilder: (context, index) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
child: Column(
children: [
InkWell(
onTap: () {
setState(() {
uploadedFilesList[index] = null;
});
},
child: Icon(Icons.close)),
Image.file(
uploadedFilesList[index],
width: MediaQuery.of(context).size.width * .25,
height: MediaQuery.of(context).size.height * .2,
),
],
),
),
);
},
),
),
谁能告诉我我的代码哪里出了问题?
在您的 setState 中,您正在设置 uploadedFilesList[index] = null;这意味着您尝试使用以下方式显示的列表中存在空值:
Image.file(uploadedFilesList[index], ....)
基本上,错误是说您将 null 传递给图像提供者。
要修复它,我认为您应该使用以下方法删除该索引处的元素:
setState(() => uploadedFilesList.removeAt(index));
而不是将其设置为空。
setState(() {
uploadedFilesList[index] = null;
});
那不应该为空
用任何虚拟图像替换 null 或用空容器替换图像提供程序。 您不能为图像提供程序提供空值。
uploadedFilesList[index]==null?Image.file(
uploadedFilesList[index],
width: MediaQuery.of(context).size.width * .25,
height: MediaQuery.of(context).size.height * .2,
):Container(
width: MediaQuery.of(context).size.width * .25,
height: MediaQuery.of(context).size.height * .2,),