Flutter Firestore: How to update nested lists? [duplicate] - firebase

This question already has answers here:
Firestore Update single item in an array field
(2 answers)
Is there any way to update a specific index from the array in Firestore
(3 answers)
Closed 3 years ago.
I'm trying to implement firestore into my flutter app. My documents on firestore have an array field which has objects. These objects have an array too. It looks like this:
Topics[
{
"id": "1234"
"questions": [] <-- This field I want to update
}
]
I know that I can update fields with the updateData() function of myDocument.reference. There I can update my value inside an array like this:
mydocument.reference.updateData({
"topics.id": 1
})
This works fine for easy structures but what I need is to get a specific index of the topics array where I get the object an its questions array which I want to update. Something like this:
document.reference.updateData({
"topics[$index].questions": FieldValue.arrayUnion([q])
});

Related

how can i add value to specific index in firstore ListField value [duplicate]

This question already has answers here:
Is there any way to update a specific index from the array in Firestore
(3 answers)
Firestore Update single item in an array field
(2 answers)
How to update an array item in Firestore
(2 answers)
Updating an item inside an array on Firebase Flutter
(1 answer)
Closed 7 months ago.
I have the following list field in Firestore.
Now, I need to place value in seetsList[2].
FirebaseFirestore.instance.collection('Rooms').doc(widget.roomId).update({
'seetsList': FieldValue.arrayUnion(?????????????????)
});
Note: my listValue is always fixed with 5 index and peer index it has default value 0
Is this possible?
There is no way you can update an element that exists in an array by its index. This means you’ll need to:
Read the content of the array into your application code.
Update the element in the array using the desired index.
Write the entire array back to Firestore.
This resource might also help.

how to change one element value of specific index of array in firebase uisng flutter? [duplicate]

This question already has answers here:
Is there any way to update a specific index from the array in Firestore
(3 answers)
Closed 2 years ago.
i want to change taken value to true on specific index of notification time stamp (array in fire base ,shown in image)so how i write my query
[1]: https://i.stack.imgur.com/aGWpI.jpg
my code:-
await FirebaseFirestore.instance
.collection('Prescriptions')
.doc(medicineDetails['id'].toString())
.update({
'NotificationTimeStamp ': FieldValue.arrayUnion([{}])
});
you can only add or remove from an array, but as a work around you can do this by changing the array in code the replacing the array in firebase.

how to edit an array element present in firestore [duplicate]

This question already has answers here:
Flutter: Update specific index in list (Firestore)
(2 answers)
Flutter: How to update an Array within an Array in the firestore
(1 answer)
Closed 2 years ago.
I have an array of strings present in firestore. I want to edit the string present at a particular index. How can I do this?
For example, let the array be ["help","me","please"];
How can I change "help" to "don't help" in flutter?
The closest I've gotten to editing the array is adding an element, which I do by FieldValue.arrayunion().
Is it possible to edit an element present at a particular index? Or am I gonna have to fetch the entire array, update it locally, and then upload the entire array?
It's not possible with a single update. You have to do exactly as you said: read the document, make changes to the array in memory, write the new array back to the document.
Is it possible to edit an element present at a particular index?
This is not possible.
Or am I gonna have to fetch the entire array, update it locally, and then upload the entire array?
That's indeed one option. The other option is to use an arrayUnion() and arrayRemove(), which work if the items in the array are unique.
Also see:
Flutter: Update specific index in list (Firestore)
Flutter: How to update an Array within an Array in the firestore

Retrive all collection except one document id Firebase [duplicate]

This question already has answers here:
Firestore: how to perform a query with inequality / not equals
(7 answers)
Closed 2 years ago.
I want to retrieve all the collections except one document id. I have following query it will get all data by validating collection field. however i want to get all data except one document.
await Firestore.instance.collection('tbl').where('id',isEqualTo:textId).getDocuments().then(
(data){
dataRowCount= data.documents.length;
}
);
Firestore doesn't offer any way to exclude documents from a query. You must can only filter for known values that you're looking for, or ranges of values. If you want to skip a document, you will have to check the query results in your code and omit the one you don't want

How to get a names List of collections indexed in a collection, in firebase in Flutter? [duplicate]

This question already has answers here:
Firebase (2016) Shallow Query
(2 answers)
Closed 3 years ago.
I have the next datas on real time firebase database:
example
As you see, there are more data in "animals" , "rocks" and "vegetables". I want get that names in a List of data, something like this:
List list = ["animals","rocks", "vegetables"];
I know how to do a get http request in flutter to get data doing this:
var response = await http.get('https://mydb.firebaseio.com/prueba.json');
pruebaFirebase = json.decode(response.body);
But it returns a List of collections, and I only need the name of each one names of collections.
Thanks in advance dear community.
Use the shallow query parameter of the Realtime Database REST API:
https://mydb.firebaseio.com/prueba.json?shallow=true

Resources