findAll method

Future<List<T>> findAll({
  1. bool? remote,
  2. bool? background,
  3. Map<String, dynamic>? params,
  4. Map<String, String>? headers,
  5. bool? syncLocal,
  6. OnSuccessAll<T>? onSuccess,
  7. OnErrorAll<T>? onError,
  8. DataRequestLabel? label,
})
inherited

Implementation

Future<List<T>> findAll({
  bool? remote,
  bool? background,
  Map<String, dynamic>? params,
  Map<String, String>? headers,
  bool? syncLocal,
  OnSuccessAll<T>? onSuccess,
  OnErrorAll<T>? onError,
  DataRequestLabel? label,
}) async {
  remote ??= _remote;
  background ??= false;
  syncLocal ??= false;
  params = await defaultParams & params;
  headers = await defaultHeaders & headers;

  label = DataRequestLabel('findAll', type: internalType, withParent: label);

  late List<T> models;

  if (!shouldLoadRemoteAll(remote!, params, headers) || background) {
    models = localAdapter.findAll().toImmutableList();
    log(label,
        'returned ${models.toShortLog()} from local storage${background ? ' and loading in the background' : ''}');
    if (!background) {
      return models;
    }
  }

  final future = sendRequest<List<T>>(
    baseUrl.asUri / urlForFindAll(params) & params,
    method: methodForFindAll(params),
    headers: headers,
    label: label,
    onSuccess: (data, label) async {
      if (syncLocal!) {
        await localAdapter.clear();
      }
      onSuccess ??= (data, label, _) async {
        final result = await this.onSuccess<List<T>>(data, label);
        return result as List<T>;
      };
      return onSuccess!.call(data, label, this as RemoteAdapter<T>);
    },
    onError: (e, label) async {
      onError ??= (e, label, _) async {
        final result = await this.onError<List<T>>(e, label);
        return result as List<T>;
      };
      return onError!.call(e, label, this as RemoteAdapter<T>);
    },
  );

  if (background) {
    // ignore: unawaited_futures
    future.then((_) => Future.value(_));
    return models;
  } else {
    return await future ?? <T>[];
  }
}