Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter & Firebase: Remove Item(Map) from Array in firebase

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).

enter image description here

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 :)

like image 301
MaxS Avatar asked May 24 '20 20:05

MaxS


People also ask

What is Flutter used for?

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.

What language is Flutter?

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.

Is Flutter a frontend or backend?

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.


2 Answers

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

like image 91
MaxS Avatar answered Oct 24 '22 06:10

MaxS


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.

like image 35
Ran Avatar answered Oct 24 '22 04:10

Ran