Want to get all from a Firestore collection in Flutter? You can achieve this by using this StreamBuilder. Here is an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | import 'package:flutter/material.dart'; import 'package:firebase_firestore/firebase_firestore.dart'; class ExpenseList extends StatelessWidget { @override Widget build(BuildContext context) { return new StreamBuilder<QuerySnapshot>( stream: Firestore.instance.collection("expenses").snapshots, builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) { if (!snapshot.hasData) return new Text("There is no expense"); return new ListView(children: getExpenseItems(snapshot)); }); } getExpenseItems(AsyncSnapshot<QuerySnapshot> snapshot) { return snapshot.data.documents .map((doc) => new ListTile(title: new Text(doc["name"]), subtitle: new Text(doc["amount"].toString()))) .toList(); } } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.