arrayUpdate static method

Future<void> arrayUpdate({
  1. required DocumentReference<Object?> ref,
  2. required String field,
  3. required List values,
  4. bool add = true,
})

Update the array in the field of the Document ref with the given List of values.

If add is true, the values will be added to ref and removed otherwise.

Implementation

static Future<void> arrayUpdate({
  required DocumentReference ref,
  required String field,
  required List<dynamic> values,
  bool add = true,
}) async {
  assert(field.isNotEmpty);
  assert(values.isNotEmpty);
  if (add) {
    return await ref.update({
      field: FieldValue.arrayUnion(values),
    });
  } else {
    return await ref.update({
      field: FieldValue.arrayRemove(values),
    });
  }
}