initialize method

  1. @override
Future<void> initialize({
  1. dynamic data,
  2. String userId = "",
})
override

Initializes the provider. Should set initialized to true at the very end of the implementation.

Implementation

@override
Future<void> initialize({dynamic data, String userId = ""}) async {
  if (initialized) {
    return;
  }

  await _service.repository.initialize();

  this.userId = userId;

  final allData = await _service.getAll(userId: userId);
  if (allData.isNotEmpty) {
    _data = allData.first;
  } else if (createIfDontExist) {
    T newData = factoryFunc().copyWith(userId: userId) as T;
    newData = newData.copyWith(id: await _service.create(newData)) as T;

    _data = newData;
  } else {
    _data = const InvalidObject();
  }

  initialized = true;
}