createNew method

Future<(bool, String?)> createNew(
  1. T newData
)

Adds a new instance of T, newData, to the repository, and replaces _data. If the creation is sucessful, will return true and the assigned id of the instance. If the creation is not successful, will return false and null.

Note: userId is automatically applied to newData.

Implementation

Future<(bool, String?)> createNew(T newData) async {
  if (!isInstanceValid(newData)) {
    return (false, null);
  }

  if (assignUserIdOnCreate) {
    newData = newData.copyWith(userId: userId) as T;
  }

  _data = newData.copyWith(id: await service.create(newData));

  notifyListeners();

  return (true, _data.id);
}