我正在使用Flutter image_picker库来实现一个功能,用户可以选择一个图像并将其上传到后端服务器。一旦他们点击选择一个图像,一个对话框应该询问他们是否要从图库中加载图像或拍摄一个。
然而,根据我的实现,我收到了一个奇怪的异常,
第一个月
我已经检查了我的功能,但我不明白问题在哪里。你能帮助指导吗?下面是代码。
非常感谢您!
List<File?> images = List.generate(3, (index) => null);
Future<void> _getImage(int index) async {
XFile? image = await showDialog<XFile?>(
context: context,
builder: (context) => const ImageSourceDialog(),
);
if (image != null) {
setState(() {
images[index] = File(image.path);
});
}
}
class ImageSourceDialog extends StatelessWidget {
const ImageSourceDialog({super.key});
@override
Widget build(BuildContext context) {
return AlertDialog(
title: const Text("Choissisez une source"),
content: Row(
children: [
Expanded(
child: ListTile(
dense: true,
leading: const Icon(Icons.camera),
title: const Text("Prendre une photo"),
onTap: () {
Navigator.of(context).pop(
ImagePicker().pickImage(source: ImageSource.camera),
);
},
),
),
Expanded(
child: ListTile(
dense: true,
leading: const Icon(Icons.photo),
title: const Text("Choisir une photo"),
onTap: () {
Navigator.of(context).pop(
ImagePicker().pickImage(source: ImageSource.gallery),
);
},
),
)
],
),
);
}
}
// this is the widget in the stateful class that triggers the upload function
iconButton(onPress: () {_getImage(index)}, ...);
字符串
1条答案
按热度按时间pvcm50d11#
字符串
在await文件之前使用await(image.path)