从Flutter的Firestore收藏中获取全部 [英] Get all from a Firestore collection in Flutter

查看:65
本文介绍了从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

推荐答案

使用 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆