creating listview with getting data from firestore on flutter

Viewed 4712

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

enter image description here

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();
  }
 }
3 Answers

Problem here is the specific instance that u r accessing is map so u might want to do this:

title: new Text(doc["Item 9"]["itemName"]),
subtitle: new Text(doc["Item 9"]["quantity"]))

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);
  }
}

Firestore doesn't support the type of query that you would need, that would be of you reading the entire "father" collection, so you can read the documents and subcollections. Queris on Firestore are shallow, so they don't permit this much functionalities. As it was informed and commented by others from the Community, it would be better for you to change your database, so it follows a pattern, instead of items having the possibility of having different names every time.

Related