【问题标题】:How to display an Image when user Click on button Flutter用户单击按钮 Flutter 时如何显示图像
【发布时间】:2021-10-14 12:57:39
【问题描述】:

我一直试图让用户按下按钮将他/她的图像上传到 Firebase 存储。它起作用了,因为我打印了结果。

flutter: https://firebasestorage.googleapis.com:443/v0/b/libraryapp-a805e.appspot.com/o/userPFP%2FIMG_0008.JPG?alt=media&token=fee1f918-5608-467d-8889-0f22853f02b1

这是函数的代码:

 Future uploadFile() async {
    if (file == null)
      return;
    else {
      final fileName = Path.basename(file!.path);
      final destination = 'userPFP/$fileName';
      var task = DatabaseServices.uploadFile(destination, file!);
      if (task == null)
        return;
      else {
        final snapshot = await task.whenComplete(() {});
        final urlDownload = await snapshot.ref.getDownloadURL();
        imageUrl = urlDownload;
        print(imageUrl);

      }
    }
  }

当我按下一个按钮时,我调用了这个函数,这个:

child: TextButton(
    onPressed: () async {
    selectFile();
    uploadFile();
    print(
      'File Selected Succesful');
    },

但图像不会立即显示。我必须关闭表单,然后再次打开它,然后图像显示。但我希望每次按下该按钮时都显示图像。

任何帮助表示赞赏!谢谢。

【问题讨论】:

    标签: firebase flutter dart firebase-realtime-database google-cloud-storage


    【解决方案1】:

    你会在上传完成后尝试调用重建吗?

    child: TextButton(
        onPressed: () async {
          selectFile();
          await uploadFile();
          print(
            'File Selected Succesful');
          },
          setState(() {});
        }
    )
    

    【讨论】:

      【解决方案2】:

      使用

      setState(() {
         imageUrl = urlDownload;
      });
      

      【讨论】:

        猜你喜欢
        • 2017-11-23
        • 2012-04-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-17
        • 1970-01-01
        相关资源
        最近更新 更多