title: How to cache Firebase data in Flutter?
body: <p>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.</p>
  
  <p>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?</p>
  
  <p>Heres a redacted version of my code:</p>
  
  <pre class="lang-dart prettyprint-override"><code>class Schedule extends StatefulWidget implements AppPage {
    final Color color = Colors.green;
    @override
    _ScheduleState createState() =&gt; _ScheduleState();
  }
  
  class _ScheduleState extends State&lt;Schedule&gt; {
    List&lt;Event&gt; events;
    List&lt;Event&gt; 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 &lt; 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) =&gt; a.start.compareTo(b.start));
            dayEvents = events.where((Event e) {
              return e.start.day == currentDay;
            }).toList();
            return ListView.builder(
              itemBuilder: itemBuilder,
              itemCount: dayEvents.length,
            );
          },
        ),
      );
    }
  }
  </code></pre>