我在项目中设置了Firestore。我创建了名为的新集合categories。在这个集合中,我创建了三个带有uniq id的文档。现在,我想在Flutter应用程序中获取此集合,因此我创建了CollectionReference:
categories
CollectionReference
Firestore.instance.collection('categories')
但我不知道接下来会发生什么。
我正在使用这个插件 firebase_firestore: 0.0.1+1
firebase_firestore: 0.0.1+1
使用 StreamBuilder
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(); } }