add method

Future<void> add(
  1. T newItem, {
  2. bool isQuiet = false,
  3. bool addToTheStart = false,
})

Add a new item to the end of data list.

Set isQuiet = true to avoid rendering loading state, default false.

Set addToTheStart = true to add the new item to the start of the list, default false.

Implementation

Future<void> add(
  T newItem, {
  bool isQuiet = false,
  bool addToTheStart = false,
}) async {
  _error = null;
  _isAdding = true;
  if (_isMounted && !isQuiet) notifyListeners();

  try {
    final result = await onCreate(newItem);
    if (addToTheStart) {
      _data.items.insert(0, result);
      _data.totalItem += 1;
    } else {
      _data.items.add(result);
      _data.totalItem += 1;
    }
    if (_awaitListener) {
      await onCreateCompleted(result);
    } else {
      onCreateCompleted(result);
    }
  } catch (e) {
    _error = e;
    if (_awaitListener) {
      await onCreateFailed(e);
    } else {
      onCreateFailed(e);
    }
  }

  _isAdding = false;
  if (_isMounted && !isQuiet) notifyListeners();
}