【问题标题】:How to properly retrieve and store data to cache flutter如何正确检索和存储数据以缓存颤振
【发布时间】:2019-09-15 17:27:59
【问题描述】:

在下面的代码中,我检索了 Firebase 存储图像的下载链接,下载了带有链接的图像并保存到临时存储(缓存)。图像文件未按 Image 中的要求加载。网络小部件立即。当我重新启动应用程序时,它只会从缓存中获取文件。为什么应用需要重启才能更新缓存内容?为什么我不能立即访问缓存中的下载文件?

return FutureBuilder(
                    future: FirebaseStorage.instance
                        .ref()
                        .child(snapshot.data["sellerID"])
                        .child(snapshot.data["image"])
                        .getDownloadURL(),
                    builder: (context, snap) {
                      if (snap.hasData) {
                        writeToFile(snapshot.data);
                        return Image.network(snap.data);
                      }
                      return Image.file(
                          File(
                              "${MyApp.baseDirGlobal}/${snapshot.data["image"]}"),
                          fit: BoxFit.cover);
                    },
                  );

writeTofile 函数定义如下

void writeToFile(DocumentSnapshot snapshot) async {
   final File file = File('${MyApp.baseDirGlobal}/${snapshot.data["image"]}');
   if (await file.exists()) {
      print("THE FILE EXISTS");
   } else {
      FirebaseStorage.instance
          .ref()
          .child(snapshot.data["sellerID"])
          .child(snapshot.data["image"])
          .writeToFile(file);
   }
}

【问题讨论】:

    标签: android firebase flutter firebase-storage


    【解决方案1】:

    writeToFile 是异步的,所以在返回 Image.network 之前需要等待。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-24
      • 1970-01-01
      • 2021-04-25
      • 2022-06-17
      • 2020-08-13
      相关资源
      最近更新 更多