0

I have a function that allows a user to change their username, simply using firebase update({}). Every time a user sends a message in my chat page, it saves it as a documents on firebase with related data, user name, time message was sent etc.

The function below is for a chat page, which works how I want it to. it takes whatever the current users username and updates all past messages with the users new current username. I want to know if their is a better way to achieve this.

How can I change a single value in any amount of documents, which obviously have different ids, in one single call?

enter image description here

List<String> testList = []; //<--- List of document IDs of previous messages user has sent
 FirebaseFirestore.instance
        .collection('users')
        .doc(loggedInUser.uid)
        .update({'messageID': testList});

    for (var item in testList) {
      var collection = FirebaseFirestore.instance
          .collection("chatrooms")
          .doc(chatroom)
          .collection("users")
          .doc('ggg')
          .collection("userMessage");
      collection
          .doc(item) // <-- Doc ID where data should be updated.
          .update({'username': loggedInUser.userName.toString()}) // <-- Updated data
          .then((_) => print('Updated'))
          .catchError((error) => print('Update failed: $error'));

0 Answers0