replace method

bool replace(
  1. Modelable model, {
  2. bool addIfMissing = true,
})

Replace the first Modelable in a list that has the same id as model. model is inserted at the same index than the previous element.

If addIfMissing is true, model will be added at the end of the list, if the list has no Modelable with the same id.

Returns true if the list contained an element with the same id as model.

Implementation

bool replace(Modelable model, {bool addIfMissing = true}) {
  bool wasInList = true;

  final index = indexWhere((element) => element.id == model.id);

  if (index != -1) {
    wasInList = false;

    removeAt(index);
    insert(index, model);
  } else if (addIfMissing) {
    add(model);
  }

  return wasInList;
}