【问题标题】:Cast Future<File> to base64 image with Flutter使用 Flutter 将 Future<File> 转换为 base64 图像
【发布时间】:2020-11-16 09:48:55
【问题描述】:

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

尝试将名称更正为现有 getter 的名称,或定义 一个名为“路径”的 getter 或字段。 print("\n 想象一下: "+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,
          );
        }
      },
    );
  }

【问题讨论】:

  • 您的变量imageFileFuture&lt;File&gt;,而不是File,因此它没有路径。转到程序中将Future 解析为实际File 的位置,然后对文件执行任何您想做的操作。
  • 这能回答你的问题吗? What is a Future and how do I use it?
  • @nvoigt 好的,但是如何实现图像的加载路径?

标签: image flutter dart base64


【解决方案1】:

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

通过

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

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

2。如果您需要小部件中的路径

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-01
    • 2019-11-05
    • 1970-01-01
    • 2015-12-09
    • 1970-01-01
    • 1970-01-01
    • 2016-09-22
    • 2021-07-19
    相关资源
    最近更新 更多