get list from fireStore in flutter app with indexes

Viewed 5561

I'm trying to get a list from firestore but i had a problem with indexes.

This is the structure of the normal Items on the List and every object contain a list of items:

class RestaurantFoodList { 
    final String id ; 
    final String title; 
    List <RestaurantitemList> items ; 
    final String imageUrl;

    RestaurantFoodList({this.id,this.title,this.items,this.imageUrl}); 
}

This is the structure of the items themselfs:

class RestaurantitemList { 
    final String id ; 
    final String title ; 
    final String imageUrl ; 
    final String description ; 
    final int price ; 
    final int shippingPrice;
    List<Checkbox> cheker;

    RestaurantitemList({this.id,this.title,this.imageUrl,this.description,this.price,this.shippingPrice,this.cheker});
} 

This is how i add the object in firestore:

Future<void> addLitem(RestaurantFoodList foodList, RestaurantitemList itemList) async {
    final _fireStore = Firestore.instance;
    _fireStore.collection('restaurant').add({
        'title': foodList.title,
        'imageUrl': foodList.imageUrl,
        'items': [
            {
                'id': itemList.id,
                'title': itemList.title,
                'imageUrl': itemList.imageUrl,
            }
         ],
    });
} 

and this is my firestore data, my problem is how can i fetch All list and their items correctly, i tried this but it didn't work:

Future<void> fetchAndSetList() async {
    final List<RestaurantFoodList> loadedList = [];

    await Firestore.instance.collection("restaurant").getDocuments().then(
          (QuerySnapshot snapshot) => snapshot.documents.forEach(
            (f) => loadedList.insert(
              0,
              RestaurantFoodList(
                  id: f.data['id'],
                  imageUrl: f.data['imageUrl'],
                  title: f.data['title'],
                  items: [f.data['items']]

              ),
            ),
          ),
    );

    notifyListeners();
    _restaurantItems[3].itemsList = loadedList;

} 

note : if i write another method to get Items i must write

items: loadedItem.add(RestaurantitemList(
    id : f.data['items'][0]['id']
))

but i want to get all the list of items not just one item so the approach of the index here i think it's a mistake.

1 Answers

I have deleted the first answer and I am adding a new one (will edit this line out once you see it).

So, there are a few problems with your fetchAndSetList function, and I rewrote it:

Future < void > fetchAndSetList() async {
    final List < RestaurantFoodList > loadedList = [];
    final List < RestaurantitemList > itemList = [];

    await Firestore.instance.collection("restaurant").getDocuments().then(
        (QuerySnapshot snapshot) => snapshot.documents.forEach((f) => {
            itemList = [];
            f.data['items'].forEach((i) =>
                itemList.add(RestaurantitemList(
                    id: i.id,
                    title: i.title,
                    imageUrl i.imageUrl
                    // you might need to add all fields here, even if they are null
                )
            ); 
            loadedList.add(RestaurantFoodList(
                id: f.data['id'],
                imageUrl: f.data['imageUrl'],
                title: f.data['title'],
                items: itemList
            ))
        });
    );

    notifyListeners();

}

What have I changed?

  • Added the itemList variable, a list of RestaurantitemList that you will add to your final loadedList;
  • Added to the original forEach a second forEach that populates the itemList with your RestaurantitemList objects, I also added a comment stating that you should add all the fields that you have mapped on the RestaurantitemList object;
  • I changed the loadedList.insert to loadedList.add, so you don't have to worry about what position the record is being added to.

NOTE: this is untested, but it should fix the issues that were having.

Related