提问人:Celaro 提问时间:11/13/2023 更新时间:11/15/2023 访问量:65
在 iOS 和 Android 上拾取和预览图像时出现问题
Problem picking and previewing an image on iOS and Android
问:
我正在使用 Flutter image_picker库来实现一项功能,用户可以在其中选择图像并将其上传到后端服务器。一旦他们单击选择图像,一个对话框应该会询问他们是要从图库中加载图像还是拍摄图像。
但是,根据我的实现,我收到了一个奇怪的异常
TypeError (type 'Future<XFile?>' is not a subtype of type 'XFile?' of 'result')
我已经检查了我的功能,但我没有看到问题出在哪里。你能帮忙指导吗?以下是代码。
谢谢!
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)}, ...);
答:
-2赞
Coding Express
11/13/2023
#1
`import 'dart:io';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
class MyImagePick extends StatefulWidget {
const MyImagePick({super.key});
static List<File?> images = List.generate(3, (index) =>
null);
@override
State<MyImagePick> createState() => _MyImagePickState();
}
class _MyImagePickState extends State<MyImagePick> {
@override
Widget build(BuildContext context) {
return Scaffold(appBar: AppBar(),
body: Column(children: [ElevatedButton(onPressed: (){
_getImage(0,context);
}, child: Text("pick image"))],),
);
}
}
Future<void> _getImage(int index,context) async {
XFile? image = await showDialog<XFile?>(
context: context,
builder: (context) => const ImageSourceDialog(),
);
if (image != null) {
MyImagePick.images[index] =await 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),
);
},
),
)
],
),
);
}
}`
在 await 文件(image.path) 之前使用 await
评论
0赞
Coding Express
11/13/2023
ElevatedButton(onPressed: ()async{ final ImagePicker picker = ImagePicker();XFile ?imageFile=await picker.pickImage(source: ImageSource.gallery);print(imageFile?.名称);}, child: Text(“pick image”)),),);
0赞
Jesse
11/13/2023
请不要在评论中加入部分答案。您可以编辑您的答案。另外,请格式化您的答案,以便于阅读。
0赞
Celaro
11/14/2023
@CodingExpress您的建议没有解决我面临的错误。甚至不需要向 File(image.path) 函数添加 await
评论