I'm currently struggling to remove a map item from a list in firebase. E.g. I want to remove item "0" from the list (completely).
I add the single array items in another place via FieldValue.arrayUnion with the updateData Method. This works perfectly fine. In another place in my I then want to have the possibility to remove the item from a list. I load the data via StreamBuilder and display ListTiles via ListView.builder. Now there I tried to replace arrayUnion by arrayRemove, but it doesn't work and I can't figure out why. Here's my code:
onPressed: () async {
try {
await Firestore.instance
.collection('users')
.document(futSnap.data.uid)
.collection('trainings')
.document(widget.trainingID)
.updateData(
{
'trainingTitle': widget.trainingTitle,
'trainingID': widget.trainingID,
'trainingDate': widget.trainingDate,
'category': widget.trainingCategory,
'trainingExercises':
FieldValue.arrayRemove(
[
{
'title': trainingExercises[i]
['title'],
'description':
trainingExercises[i]
['description'],
'imageURL': trainingExercises[i]
['imageUrl'],
'repetitions':
trainingExercises[i]
['repetitions'],
},
],
)
},
);
} catch (e) {
print(e);
}
},
I don't receive any error or something.. Still a beginner to flutter and programming, so help would be highly appreciated :)
Flutter is Google's free and open-source UI framework for creating native mobile applications. Released in 2017, Flutter allows developers to build mobile applications with a single codebase and programming language. This capability makes building both iOS and Android apps simpler and faster.
Dart platform Flutter apps are written in the Dart language and make use of many of the language's more advanced features. While writing and debugging an application, Flutter runs in the Dart virtual machine, which features a just-in-time execution engine.
Flutter is a popular frontend development framework from Google that enables developers to build beautiful frontends for any screen. Flutter is designed to streamline cross-platform app development while maintaining a consistent user experience.
Thanks for your replies, in trying further, it seems that this actually works now:
onPressed: () async {
try {
await Firestore.instance
.collection('users')
.document(futSnap.data.uid)
.collection('trainings')
.document(widget.trainingID)
.updateData(
{
'trainingExercises':
FieldValue.arrayRemove(
[trainingExercises[i]],
)
},
);
} catch (e) {
print(e);
}
},
But to be fair I don't understand completely why, because my understanding was as well as yours, that the information has to match the whole map..? I'm happy that it works, but it would be awesome to understand why :D
List<ExpenseData> deleteExpenseData = [];
deleteExpenseData.add(spendingDetailMapList[documentIndex][documentId[documentIndex]][spending]);
expenseCR.doc(documentId[documentIndex]).update({"spending": FieldValue.arrayRemove([deleteExpenseData[0].toJson()],)});
I did this and It works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With