0

Hi the flutter code below loads a photo from the camera roll and then displays it, on video, what I have to do is recover the path of the file, to do it I use the code below inside the inserimento function but when I run the code I the following error:

Try correcting the name to the name of an existing getter, or defining a getter or field named 'path'. print("\n Immagine: "+imageFile.path);

Flutter Code:

Future<File> imageFile; 

  //Costruttore 
  ArticoloEditPage(){
    aggiornaValori();
    BackButtonInterceptor.add(myInterceptor);
    setData(new DateTime.now());
  }

  //Disabilito il bottone di back su android
  bool myInterceptor(bool stopDefaultButtonEvent, RouteInfo info) {
    return true;
  }
  //Funzione di init
  void init() {
    aggiornaValori();
    BackButtonInterceptor.add(myInterceptor);
  }

  //Funzione che esegue la creazione dell'utente 
  Future<bool> inserimento(BuildContext context) async {
   print("\n Immagine: "+imageFile.path);
  // var base64File=await Supporto.castDocumentToBase64(imgPath);
   //print("\n Immagine in base64: "+imgPath);
  }
  pickImageFromGallery(ImageSource source) {
    setState(() {      
      imageFile = ImagePicker.pickImage(source: source);
    });
  }
 
  Widget showImage() {
    return FutureBuilder<File>(
      future: imageFile,
      builder: (BuildContext context, AsyncSnapshot<File> snapshot) {
        if (snapshot.connectionState == ConnectionState.done &&
            snapshot.data != null) {
          return Image.file(
            snapshot.data,
            width: 300,
            height: 300,
          );
        } else if (snapshot.error != null) {
          return const Text(
            'Errore caricamento non riuscito',
            textAlign: TextAlign.center,
          );
        } else {
          return const Text(
            'Nessuna immagine selezionata',
            textAlign: TextAlign.center,
          );
        }
      },
    );
  }
RaSha
  • 1,356
  • 1
  • 16
  • 30
riki
  • 1,502
  • 5
  • 17
  • 45
  • Your variable `imageFile` is a `Future`, not a `File`, so it does not have a path. go to a place in your program where the `Future` is resolved to an actual `File` and do whatever you want to do with the file. – nvoigt Nov 16 '20 at 10:40
  • 1
    Does this answer your question? [What is a Future and how do I use it?](https://stackoverflow.com/questions/63017280/what-is-a-future-and-how-do-i-use-it) – nvoigt Nov 16 '20 at 10:40
  • @nvoigt ok but how implement load path of image? – riki Nov 16 '20 at 10:41

1 Answers1

1

you can not get path directly in future method, so

by

1. this you can print your path.

  Future<bool> inserimento(BuildContext context) async {
    var pathData=await imageFile;
    print("\n Immagine: "+pathData.path);
  }

or

2. if you need path in widget

Widget showImage() {
        return FutureBuilder<File>(
          future: imageFile,
          builder: (BuildContext context, AsyncSnapshot<File> snapshot) {
            if (snapshot.connectionState == ConnectionState.done &&
                snapshot.data != null) {


              print("Your Path : "+snapshot.data.path);


              return Image.file(
                snapshot.data,
                width: 300,
                height: 300,
              );
            } else if (snapshot.error != null) {
              return const Text(
                'Errore caricamento non riuscito',
                textAlign: TextAlign.center,
              );
            } else {
              return const Text(
                'Nessuna immagine selezionata',
                textAlign: TextAlign.center,
              );
            }
          },
        );
      }

also

3. if you need base64 image. then

Future<bool> inserimento(BuildContext context) async {
     var pathData=await imageFile;
     var base64Image = base64Encode(pathData.readAsBytesSync());

        print("\n Immagine base64Image: "+base64Image.toString());
  }
Shirsh Shukla
  • 5,491
  • 3
  • 31
  • 44