How to cache Firebase data in Flutter?

11,025

Solution 1

To be sure whether the data is coming from Firestore's local cache or from the network, you can do this:

          for (int i = 0; i < snapshot.data.documents.length; i++) {
            DocumentSnapshot doc = snapshot.data.documents.elementAt(i);
            print(doc.metadata.isFromCache ? "NOT FROM NETWORK" : "FROM NETWORK");

In the case you described you are probably going to still see the loading screen when its "NOT FROM NETWORK". This is because it does take some time to get it from the local cache. Soon you will be able to ask for the query's metadata for cases with empty results.

Like others suggested, you can cache the results and you won't see this. First you can try to cache it in the Widget using something like:

  QuerySnapshot cache; //**

  @override
  Widget build(BuildContext context) {
    return Center(
      child: StreamBuilder(
        initialData: cache, //**
        stream: Firestore.instance.collection('events').snapshots(),
        builder: (context, snapshot) {
          if (!snapshot.hasData) {
            return Text("Loading...");
          }
          cache = snapshot.data; //**

This will make your widget remember the data. However, if this does not solve your problem, you would have to save it not in this widget but somewhere else. One option is to use the Provider widget to store it in a variable that lives beyond the scope of this particular widget.

Probably not related, but it's also a good idea to move the Firestore.instance.collection('events').snapshots() to initState(), save the reference to the stream in a private field and use that it StreamBuilder. Otherwise, at every build() you may be creating a new stream. You should be ready for build() calls that happen many times per second, whatever the reason.

Solution 2

You can use the the following code to define the source you want to retrieve data from. This will search either in local cache or on the server, not both. It works for all get() parameters, no matter if it is a search or document retrieval.

import 'package:cloud_firestore/cloud_firestore.dart';

FirebaseFirestore.instance.collection("collection").doc("doc").get(GetOptions(source: Source.cache))

To check if the search has data in cache, you need to first run the search against cache and if there is no result, run it against the server. I found project firestore_collection to use a neat extension that can greatly simplify this process.

import 'package:cloud_firestore/cloud_firestore.dart';

// https://github.com/furkansarihan/firestore_collection/blob/master/lib/firestore_document.dart
extension FirestoreDocumentExtension on DocumentReference {
  Future<DocumentSnapshot> getSavy() async {
    try {
      DocumentSnapshot ds = await this.get(GetOptions(source: Source.cache));
      if (ds == null) return this.get(GetOptions(source: Source.server));
      return ds;
    } catch (_) {
      return this.get(GetOptions(source: Source.server));
    }
  }
}

// https://github.com/furkansarihan/firestore_collection/blob/master/lib/firestore_query.dart
extension FirestoreQueryExtension on Query {
  Future<QuerySnapshot> getSavy() async {
    try {
      QuerySnapshot qs = await this.get(GetOptions(source: Source.cache));
      if (qs.docs.isEmpty) return this.get(GetOptions(source: Source.server));
      return qs;
    } catch (_) {
      return this.get(GetOptions(source: Source.server));
    }
  }

If you add this code, you can simply change the .get() command for both documents and queries to .getSavy() and it will automatically try the cache first and only contact the server if no data can be locally found.

FirebaseFirestore.instance.collection("collection").doc("doc").getSavy();
Share:
11,025
Marco
Author by

Marco

Updated on July 18, 2022

Comments

  • Marco
    Marco almost 2 years

    In my app I build a list of objects using data from Firebase. Inside a StreamBuilder, I check if the snapshot has data. If it doesen't, I am returning a simple Text widget with "Loading...". My problem is that if I go to another page in the app, and then come back, you can see for a split second that it says 'Loading...' in the middle of the screen, and it is a bit irritating. I am pretty sure it is downloading the data from Firebase, and building the widget every time I come back to that page. And if I don't do the check for data, it gives me a data that I am trying to access data from null.

    Is there a way to cache the data that was already downloaded, and if there has been no change in the data from Firebase, then just use the cached data?

    Heres a redacted version of my code:

    class Schedule extends StatefulWidget implements AppPage {
      final Color color = Colors.green;
      @override
      _ScheduleState createState() => _ScheduleState();
    }
    
    class _ScheduleState extends State<Schedule> {
      List<Event> events;
      List<Event> dayEvents;
      int currentDay;
      Widget itemBuilder(BuildContext context, int index) {
        // Some Code
      }
      @override
      Widget build(BuildContext context) {
        return Center(
          child: StreamBuilder(
            stream: Firestore.instance.collection('events').snapshots(),
            builder: (context, snapshot) {
              if (!snapshot.hasData) {
                return Text("Loading...");
              }
              events = new List(snapshot.data.documents.length);
              for (int i = 0; i < snapshot.data.documents.length; i++) {
                DocumentSnapshot doc = snapshot.data.documents.elementAt(i);
    
                events[i] = Event(
                  name: doc["name"],
                  start: DateTime(
                    doc["startTime"].year,
                    doc["startTime"].month,
                    doc["startTime"].day,
                    doc["startTime"].hour,
                    doc["startTime"].minute,
                  ),
                  end: DateTime(
                    doc["endTime"].year,
                    doc["endTime"].month,
                    doc["endTime"].day,
                    doc["endTime"].hour,
                    doc["endTime"].minute,
                  ),
                  buildingDoc: doc["location"],
                  type: doc["type"],
                );
              }
              events.sort((a, b) => a.start.compareTo(b.start));
              dayEvents = events.where((Event e) {
                return e.start.day == currentDay;
              }).toList();
              return ListView.builder(
                itemBuilder: itemBuilder,
                itemCount: dayEvents.length,
              );
            },
          ),
        );
      }
    }
    
  • Marco
    Marco over 4 years
    "Like others suggested, you can cache the results and you won't see this." How would I actually go about doing this?
  • LOG_TAG
    LOG_TAG over 4 years
    @Marco looking for something like this for firebase ?pub.dev/packages/flutter_cache_store