updating Map values in array flutter Firebase - flutter

here is the map and the vlaue im trying to update
the problem is when i try to update the imageUrl , the whole Map replacing with only ImageUrl new value
like this
what i want is to update imageUrl only , not effecting other data
this is what i do but not work
FirebaseFirestore.instance.collection('oorders').doc('MyDocId')
.update({ 'items.0.imageUrl' : 'new image Url' });

From the provided Firestore Database structure it is clear that you are trying to update an element inside an array field in a Firestore document. But currently Firestore doesn't have the ability to update an existing element in an indexed array. It supports only two methods, arrayUnion() to add an element and arrayRemove() to remove an element from the array as mentioned in the documentation.
So if you try to do something like the following -
FirebaseFirestore.instance
.collection('oorders')
.doc('MyDocId')
.update({'items.0.imageUrl': 'new image Url'});
it will delete the array element items and create another map named items with the field mentioned in the update statement.
As an alternative, you can read the entire array out of the document, make modifications to it in memory, then update the modified array field entirely. This is an error prone and tedious process. You can have a look at this article to know more about it.
I am not sure of your use case, but it seems you can make your database structure more suitable by using maps(nested objects) instead of arrays. Something like this -
By doing this you can update the nested objects by dot notation as mentioned here which will update only the mentioned field without deleting the other fields. The sample to update the document will look like this -
var item = 'item1';
var propertyToBeUpdated = 'imageUrl';
var newImageUrl = 'New image Url';
FirebaseFirestore.instance
.collection('collectionId')
.doc('documentId')
.update({'items.$item.$propertyToBeUpdated': newImageUrl});

Related

Reading and updating array of reference type in Firestore

I am using Firestore in flutter. I have a field in document that is an array of reference type. I want to read it and further want to add new values to this array but I am not sure how to do it though I am able read the reference type element value as a string. I am stuck here. Can anyone guide me how to do it. Below is the code with the screenshots of structure and data.
DocumentReference docRef = docRef = widget.function.variants;
If this is a map of document references, and you want to append to that map,
you'll need to:
Create the relevant doc in Firestore (if not yet created)
Get the reference to that doc (using firebase doc() function) -
of type DocumentReference
Add that reference to your map
Save back to Firestore.
Although you are able to cast these values to string, they are actually references to other documents in your Firestore DB. new values, should be added as such.

add string to an array in Firestore - flutter

i have created an array of strings "challengeMember" in Firestore database and i want to add data to it when user clicks a button, my database:
in my code am trying to update the array, every time user join a challenge, the challenge name will be added to this array i know how to access the array using:
FirebaseFirestore.instance.collection("Users").doc(user!.uid).update({'challengeMember': widget._challengeName});
but the problem with this it is not specifying the array index and it is not checking whether the index is empty or not, so how can i dynamically access this array and keep adding values.
Firestore has a special trick to add an array, and it goes like this:
await FirebaseFirestore.instance.collection('users').doc(uid).update({
'tokens': FieldValue.arrayUnion([token]),
});
See the FieldValue documentation on how to manipulate arrays.

How to create a collection inside a document in Firestore with Flutter?

I'm working with a document and I want to create an empty collection inside the document.
FirebaseFirestore.instance.collection('Users').doc("thisdoc). ??? (create collection inside this document)
I don't know what to add instead of ???
The documentation will help you there.
NOTE The Firebase team is migrating these docs onto firebase.com directly, so this link may get old quickly in the next months.
EDIT. Since you need to create it under a subcollection, simply:
Take a reference of that document
Reference a subcollection in that document, even if it doesn't exist, yet
Create a first document in there, and your subcollection is made.
Pseudocode:
// WARN, PSEUDOCODE AHEAD
final documentRef = firestore.collection("myCol").document("myDoc");
final subCollectRef = documentRef.collection("mySubCol");
subCollectRef.add({
'your': 'data',
});

Query a Map containing a string saved in Cloud Firestore with Dart/Flutter

I've a problem with a query condition. I would like to query a string inside an array that's also inside a map. I manage to receive the information complet without the where condition but when I do this condition I've an error.
My function to retrieve all information
My array with a map inside
If category is the only value inside the produits array items, you can check for its existence with array-contains:
collectionRef.where('produits', arrayContains({ 'categorie': 'Alimentation' }))
If there are more properties for the array item, you will need to specify all of them in the call to arrayContains. There is no way to query for a subset of the array item.
In such cases, a common workaround is to add a extra array field to the document with just the property values you want to query for. For example:
produitsCategories: ['Alimentation', '...']
With that field in place you can then query it with:
collectionRef.where('produitsCategories', arrayContains('Alimentation'))

How to increment individual element in an array in firestore for flutter

This is how my data look like.
I'm able to use FieldValue.increment(1) to update individual fields but is there a way to use FieldValue.increment(1) for specific individual elements in the array? Thanks in advance!
I tried using the following code:
firestore.collection('test').doc('I1raMaJArb1sWXWqQErE')
.update({
'rating.0': FieldValue.increment(1),
});
But the whole rating became empty as seen
You can't use FieldValue.increment() if the field is part of the array. The value of the field inside the array is fixed. The best way to update or edit the field that part of an array is to read the entire document, modify the data in memory and update the field back into the document.