I am using the Flutter image_picker library to implement a feature where users can pick an image and upload them to a backend server. Once they click on choosing an image, a dialog box should ask them if they want to load an image from the gallery or take one.
However, based on my implementation, I am receiving a weird exception of
TypeError (type 'Future<XFile?>' is not a subtype of type 'XFile?' of 'result')
I have checked my functions but i don’t see where the problem is. Could you help in directing? Below are the codes.
Thank you very much!
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
Answers
Can you give a simple sample for this page (full import, all code of this file)
use await before await File(image.path) i am very disaapointed from nagative rateing i leave stackoverflow