使用 ImagePicker 在图库中选择图像。有效,但没有显示图像。 错误:未处理的异常:类型“XFile”不是类型转换中"file"类型的子类型
在我的代码下面:
class _RegisterScreenState extends State {
File image;
String imgUrl;
final imagePicker = ImagePicker();
Future getImage() async {
File img = await imagePicker.pickImage(
source: ImageSource.gallery);
setState(() {
image = img as File;
});
}
child: CircleAvatar(
radius: 60,
backgroundColor: Colors.white,
backgroundImage: image != null
? Image.file(image)
: NetworkImage(
"https://cdn-icons-png.flaticon.com/512/1177/1177568.png"),
),
错误:未处理的异常:类型“XFile”不是类型转换中“File”类型的子类型
我在:
version: 1.0.0+1
environment:
sdk: ">=2.7.0 <3.0.0"
image_picker: ^0.8.4+2
path_provider: ^2.0.5
最佳答案
我也遇到了这个错误。我通过以下方法解决了这个问题:
File myImage;
Future getImage() async {
File img = await ImagePicker.pickImage(source: ImageSource.gallery);
setState(() {
myImage = File(img.path);
});
}
关于flutter - 选择/flutter 后 ImagePicker 图像不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69339003/