从 Flutter 中的 Firestore 集合中获取所有内容 [英] Get all from a Firestore collection in Flutter
本文介绍了从 Flutter 中的 Firestore 集合中获取所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在我的项目中设置了 Firestore.我创建了名为 categories
的新集合.在这个集合中,我创建了三个带有 uniq id 的文档.现在我想在我的 Flutter 应用程序中获取这个集合,所以我创建了 CollectionReference
:
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 collection in my Flutter application so I created CollectionReference
:
Firestore.instance.collection('categories')
但我不知道接下来会发生什么.
but I don't know what next.
我正在使用这个插件 firebase_firestore: 0.0.1+1
推荐答案
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();
}
}
这篇关于从 Flutter 中的 Firestore 集合中获取所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文