From b23543889d3dca0f9adf700c1dccd8847a9ae0cc Mon Sep 17 00:00:00 2001 From: Alvin Konda Date: Thu, 18 Oct 2018 01:18:40 +0200 Subject: [PATCH] Correct usage of StreamBuilder (#759) https://docs.flutter.io/flutter/widgets/StreamBuilder-class.html I had some problems with using just `if (snapshot.hasData)`, i thought other users might benefit from the correct usage. --- packages/cloud_firestore/README.md | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/packages/cloud_firestore/README.md b/packages/cloud_firestore/README.md index 379a5a820924..bfaba2d0fdf9 100755 --- a/packages/cloud_firestore/README.md +++ b/packages/cloud_firestore/README.md @@ -47,15 +47,20 @@ class BookList extends StatelessWidget { return StreamBuilder( stream: Firestore.instance.collection('books').snapshots(), builder: (BuildContext context, AsyncSnapshot snapshot) { - if (!snapshot.hasData) return Text('Loading...'); - return ListView( - children: snapshot.data.documents.map((DocumentSnapshot document) { - return ListTile( - title: Text(document['title']), - subtitle: Text(document['author']), + if (snapshot.hasError) + return new Text('Error: ${snapshot.error}'); + switch (snapshot.connectionState) { + case ConnectionState.waiting: return new Text('Loading...'); + default: + return new ListView( + children: snapshot.data.documents.map((DocumentSnapshot document) { + return new ListTile( + title: new Text(document['title']), + subtitle: new Text(document['author']), + ); + }).toList(), ); - }).toList(), - ); + } }, ); }