Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flutter firestore, add new object in array

I have array of objects, I want to add new object when user enter new data in the array?

Firestore.instance.collection(city).document('Attractions').updateData(                         "data", FieldValue.arrayUnion(obj)                       ); 

This shows error, How can I achieve this with flutter?

like image 828
Sami Ullah Avatar asked Dec 07 '18 07:12

Sami Ullah


People also ask

Can I add to an array firestore?

When it comes to the official documentation regarding how to update elements in an array in Firestore, it says that: If your document contains an array field, you can use arrayUnion() and arrayRemove() to add and remove elements. arrayUnion() adds elements to an array but only elements not already present.


Video Answer


1 Answers

Right Format is :

Firestore.instance.collection(city).document('Attractions').updateData({"data": FieldValue.arrayUnion(obj)}); 

updateData Take Map<String,dynamic> as data.

In your Code you are having , as separator between key - value instead it should be :

like image 160
anmol.majhail Avatar answered Sep 18 '22 17:09

anmol.majhail