initialize method

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

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

Implementation

@override
Future<void> initialize({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;

    AppEventsDispatcher().publish(DataCreatedEvent(service.repositoryId, data));
  } else {
    data = null;
  }

  initialized = true;
  AppEventsDispatcher().publish(ProviderInitializedEvent(service.repositoryId));
}