Home > Software engineering >  Reset flutter cache from firebase
Reset flutter cache from firebase

Time:07-28

I have a code that takes data from firebase. The problem is that i am caching the data once i am getting it and than checking if i cached it the next time i want to take it so i can reduce loading time.

The problem is that when i am changing something in the database, when i refresh the app i still see the old data because i think that it is getting it from the cache.

How can i make it so that when i reset the app, i clear the cache and get the new data?

here is a piece of code that checks if the data is cached or not.

  List lists = [];
  print(id);
  await firestoreInstance
      .collection("menu/"   id   "/"   id)
      .get(GetOptions(source: Source.cache))
      .then((querySnapshot) {
    querySnapshot.docs.forEach((result) async {

      lists.add(result.data()['name']
 );
    });
  });
  if (lists.isEmpty) {
    print('aici');
    await firestoreInstance
        .collection("menu/"   id   "/"   id)
        .get()
        .then((querySnapshot) {
      querySnapshot.docs.forEach((result) async {

        lists.add(result.data()['name']);

      });
    });
    return lists;
  } else {
    return lists;
  }
}

CodePudding user response:

To clear the cache, you can call the clearPersistence method, which clears both cached reads and pending writes.


I'd usually recommend against trying to do your own cache management though, and instead let Firestore handle it. This also means I wouldn't use GetOptions and instead just use a regular get, or (even better) a realtime snapshot listener which lets Firestore manage the cache best.

CodePudding user response:

you are only checking if the list is empty, you have to check if the newly added data is available in the list, if not available, it will fetch the data from firebase again and if it is available than it will show data from previously stored data list.

  • Related