2

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();
  }
 }
4
  • Right now are you returning anything from your snapshots stream Commented Apr 8, 2020 at 3:55
  • no only this page Commented Apr 8, 2020 at 4:00
  • are you getting any log error? if yes then add here. This can also happen when you do not provide permission from firebase. Commented Apr 8, 2020 at 4:02
  • i am not getting any error but i can not see anything in my app it does not show anything there is nothing wrong with permisson i can write and read data if it is not nested like this Commented Apr 8, 2020 at 4:05

3 Answers 3

1

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"]))
Sign up to request clarification or add additional context in comments.

5 Comments

but as i said above the item name could be change so i just want to get all data to the listview
may be u can try to wrap the map inside array in firebase so that u can get length of that array and u can draw list view easily
i did not get what exactly did you mean sorry
change ur database structure i mean
or try using models that my best guess
1

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

2 Comments

Sorry bout that i made a couple minor changes to it but you still getting nothing back
you might want to consider changing your map into an array here @Frank Van Puffelen suggests that heres the link stackoverflow.com/questions/54012380/…
0

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.

2 Comments

So which database should i use for my app do you have any suggestions
Hi @MehmetReşatDemir I believe Firestore can be a good choice for you, but you will need to change your data model, so it's not that "free" as it is right now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.