3

I'm experimenting with arrays and maps/objects in firestore. I wondered how can I remove a specific map from the array. I tried something like this:

await Firestore.instance.collection('users').document(interestedInID).get().then((val){
  return val.data['usersInterested'].removeWhere((item)=>
    item['userID'] == userID
  ); 
}).catchError((e){
  print(e);
});

but I get this error in terminal:

Unsupported operation: Cannot remove from a fixed-length list

I don't really understand what it means. I did some googling and fixed-length list is exactly what it says. It's a list that has a fixed length and it can't be changed, but fixed-length list has to be declared explicitly. Growable list on the other hand doesn't need to be declared. I haven't declared the fixed-length list in my firestore, yet it keeps saying that I can't remove elements from it. I can add / push elements however and remove them using:

'key': FieldValue.arrayRemove([value])

but I can't figure out how to remove the element based on a specific condition. In this case an userID.

Any suggestions? Thanks a lot!

2
  • have you found a solution to this problem? Commented May 4, 2020 at 21:43
  • Hi @Andrey. Was quite a long time ago, but I posted the solution that worked for me, but it showed I deleted it. I don't know why, so I undeleted the post. Can you see it underneath? Commented May 4, 2020 at 22:39

2 Answers 2

1

Figured it out.

await Firestore.instance.collection('users').document(interestedInID).updateData({
  'usersInterested': FieldValue.arrayRemove([{}.remove(userID)])
});

I'm not sure, but I think get() simply allows you to read the document, but doesn't allow to make any changes to it. Anyways, now it works

Sign up to request clarification or add additional context in comments.

Comments

0

This may be a workaround:

  1. You fetch your specific Array with key: values from Firebase
  2. Put that in a temporary reference List in dart
  3. Remove your specific Map from the reference List, like you did before
  4. Update data in Firebase like so:
// make a reference List
List<Map> _modifiedUsersInterested = List();

// ... (fetch your Array first from Firebase, put those items in the reference List)

// remove the right Map from the reference List
_modifiedUsersInterested.removeWhere((item) => {
    item['userID'] == userID
});

// set the reference List as your array in Firebase
await Firestore.instance
.collection('users')
.document(interestedInId)
.updateData(
    {'usersInterested': _modifiedUsersInterested}
);

1 Comment

did you actually confirm that this method works? Either way, there should be a much simpler way to do this without reading and then resaving the entire list minus the removed value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.