5

I need to add list of objects in firestore as shown in the images. i could only add two list with the below code

 onPressed: () {
        _fireStore.collection('notifyseller').document().updateData({
          'Customer': userName,
          "address": controller.text,
          "mobile": mobileNumber,
          "Item": FieldValue.arrayUnion([
            {
              "name": itemName.toList()[0],
              "price": rate.toList()[0],
              "quantity": quantity.toList()[0]
            },
           {
              "name": itemName.toList()[1],
              "price": rate.toList()[1],
              "quantity": quantity.toList()[1]
            },
          ]),
        });
      },

here itemName.toList() contains list of strings. by the above code i can only add two data. i need to add all the item in the itemName.toList() to that array, instead of giving index for each array


enter image description here

2 Answers 2

6

If you are sure that your three list have same length try this;

List yourItemList = [];
for (int i = 0; i < itemName.length; i++)
  yourItemList.add({
    "name": itemName.toList()[i],
    "price": rate.toList()[i],
    "quantity": quantity.toList()[i]
  });

_fireStore.collection('notifyseller').document().updateData({
  'Customer': userName,
  "address": controller.text,
  "mobile": mobileNumber,
  "Item": FieldValue.arrayUnion(yourItemList),
});
Sign up to request clarification or add additional context in comments.

3 Comments

bro that worked; but is there any simple way like with forEach or map functions; here i need to change my stateless to stateful widget to call that for loop in initstate
because of using different lists it's difficult to use map or foreach methods in your case.
Do you have any idea to retrieve this
2

i did this way

class Item{
String name;
String price;
String qty;

//named constructor here

Map<String, dynamic> toMap() {
    return {
      'name': name,
      'price': price,
      'qty':qty,
    };
  }
}
List<Items> items = [Item(name:"",price:"",qty:""),Item(name:"",price:"",qty:"")]
    
_fireStore.collection('notifyseller').document().updateData({
  'Customer': userName,
  "address": controller.text,
  "mobile": mobileNumber,
  "Item":, items.map<Map>((e)=> e.toMap()).toList();
});

Comments

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.