使用 Flutter 将 Future<File> 转换为 base64 图像

Cast Future<File> to base64 image with Flutter

嗨,下面的 flutter 代码从相机胶卷加载照片,然后在视频上显示它,我要做的是恢复文件的路径,为此我在 inserimento 函数但是当我 运行 代码时出现以下错误:

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

颤动代码:

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

你不能在未来的方法中直接获取路径, 所以

来自

1.这样你就可以打印你的路径了。

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

2。如果你需要 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,
              );
            }
          },
        );
      }

还有

3。如果你需要 base64 图片。 然后

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

        print("\n Immagine base64Image: "+base64Image.toString());
  }