Problem picking and previewing an image on iOS and Android

102 views Asked by At

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

There are 1 answers

3
Coding Express On
`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)