简体   繁体   中英

How to properly retrieve and store data to cache flutter

In the code below, I retrieved the download link to a firebase storage image, downloaded the image with the link and saved to temporary storage(cache). Image file does not load as required in Image. Network widget immediately. It only gets the file from cache, when I restart the app. Why does the app need to restart to update cache content? Why can't I access downloaded files in cached almost immediately?

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

the writeTofile function is defined below

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

writeToFile是异步的,因此您需要等待,然后返回Image.network。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM