updateById method

void updateById(
  1. T item, {
  2. bool pushToStart = true,
  3. bool pushToEnd = false,
})

Update a given item by comparing their respective id. If pushToStart is true, the item will be repositioned to index 0; If pushToEnd is true, the item will be repositioned to the end of the list;

Implementation

void updateById(T item, {bool pushToStart = true, bool pushToEnd = false}) {
  // Since pushToStart is the default, we swap if pushToEnd is true in case
  // pushToStart is still true, which should not be the case.
  if (pushToEnd) pushToStart = false;
  final index = _items.indexWhere((element) => element.id == item.id);
  if (index > -1) {
    if (pushToStart || pushToEnd) {
      _items.removeAt(index);
      if (pushToStart) {
        _items.insert(0, item);
      } else {
        _items.add(item);
      }
    } else {
      _items[index] = item;
    }
    notifyListeners();
  }
}