Skip to content

Commit

Permalink
Correct usage of StreamBuilder (flutter#759)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
alvinkonda authored and kroikie committed Oct 17, 2018
1 parent f0d714c commit b235438
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions packages/cloud_firestore/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,20 @@ class BookList extends StatelessWidget {
return StreamBuilder<QuerySnapshot>(
stream: Firestore.instance.collection('books').snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> 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(),
);
}
},
);
}
Expand Down

0 comments on commit b235438

Please sign in to comment.