I set up Firestore in my project. I created new collection named categories
. In this collection I created three documents with uniq id. Now I want to get this c
Here is the code if you just want to read it once
QuerySnapshot querySnapshot = await Firestore.instance.collection("collection").getDocuments();
var list = querySnapshot.documents;
I was able to figure out a solution:
Future getDocs() async {
QuerySnapshot querySnapshot = await Firestore.instance.collection("collection").getDocuments();
for (int i = 0; i < querySnapshot.documents.length; i++) {
var a = querySnapshot.documents[i];
print(a.documentID);
}
}
Call the getDocs()
function, I used build function, and it printed all the document IDs in the console.
QuerySnapshot snap = await
Firestore.instance.collection('collection').getDocuments();
snap.documents.forEach((document) {
print(document.documentID);
});
Using StreamBuilder
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();
}
}