2

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)}, ...);

1 Answer 1

-2
`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),
            );
          },
        ),
      )
    ],
  ),
);
}
 }`

use await before await File(image.path)

Sign up to request clarification or add additional context in comments.

3 Comments

ElevatedButton(onPressed: ()async{ final ImagePicker picker = ImagePicker(); XFile ?imageFile=await picker.pickImage(source: ImageSource.gallery); print(imageFile?.name); }, child: Text("pick image")),),);
Please don't put parts of your answer in the comments. You can edit your answer. Also, please format your answer so it's easier to read.
@CodingExpress your suggestion did not fix the error i am facing. There is no need to even add await to the File(image.path) function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.