使用flutter从Firestore获取数据来创建ListView [英] creating listview with getting data from firestore on flutter

查看:126
本文介绍了使用flutter从Firestore获取数据来创建ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,我需要将这些数据从firestore数据库中逐一地移到Listview上.我尝试了很多类似流生成器和将来的生成器的方法,但是由于我的数据库可以处理嵌套数据,因此无法获取,如果您有任何建议,请非常感谢.有什么解决方案可以读取所有这些嵌套数据?有时商品名称可能会有所不同,所以我可以读取嵌套数据是否具有id或数量值

I have an app I need to get those data from firestore database to the Listview on flutter one by one. I tried many things like a stream builder and future builder but I could not get because my database works with nested data if you have any suggestions please let me know thanks a lot. is there any solution to reading all this nested data? sometimes item name can be different so can I read if nested data have id or quantity value

我已经用过了,但是不起作用:

I already used this but it does not work:

  import 'package:flutter/material.dart';
  import 'package:cloud_firestore/cloud_firestore.dart';

  class cart_page extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new StreamBuilder<QuerySnapshot>(
    stream: Firestore.instance.collection("cart").where("quantity", 
   isGreaterThanOrEqualTo: 1).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["itemName"]),
        subtitle: new Text(doc["quantity"]))
  )
    .toList();
  }
 }

推荐答案

除了我上面的回答以外,我还更改了代码,您还查询了嵌套数据库错误,并且由于需要索引才能完成这项工作,因此我不得不移动一些周围的事情.您可能需要尝试使用它才能使它起作用,但是总的思路就在那里.

I changed your code around besides the answer above mine you also are querying your nested database wrong and since I need an index to make this work I had to move some things around. You might have to play with this to get it to work but the general idea is there.



class Example extends StatelessWidget {
   var listOfWidgets = [];
  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemCount: //number of items ,
      itemBuilder: (BuildContext context, int index) => StreamBuilder(

          stream: Firestore.instance
              .collection("cart")
              .where("Item$index.quantity", isGreaterThanOrEqualTo: 1)
              .snapshots(),
          builder:
              (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
                  if (!snapshot.hasData) return new Text("There is no expense");
                   return ...listOfWidgets;
              }),
    );
  }

  getExpenseItems(AsyncSnapshot<QuerySnapshot> snapshot) {

    var item = snapshot.data.documents
        .map((doc) => new ListTile(
            title: new Text(doc["Item$index.Name"]),
            subtitle: new Text(doc["Item$index.quantity"])))
        .toList();
    listOfWidgets.add(item);
  }
}

这篇关于使用flutter从Firestore获取数据来创建ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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