findAll method
Future<List<T> >
findAll({
- bool remote = true,
- bool background = false,
- Map<
String, dynamic> ? params, - Map<
String, String> ? headers, - bool syncLocal = false,
- OnSuccessAll<
T> ? onSuccess, - OnErrorAll<
T> ? onError, - DataRequestLabel? label,
inherited
Returns all models of type T
.
If shouldLoadRemoteAll (function of remote
) is true
,
it will initiate an HTTP call.
Otherwise returns all models of type T
in local storage.
Arguments params
and headers
will be merged with
defaultParams and defaultHeaders, respectively.
For local storage of type T
to be synchronized to the exact resources
returned from the remote source when using findAll
, pass syncLocal: true
.
This call would, for example, reflect server-side resource deletions.
The default is syncLocal: false
.
See also: urlForFindAll, methodForFindAll.
Implementation
Future<List<T>> findAll({
bool remote = true,
bool background = false,
Map<String, dynamic>? params,
Map<String, String>? headers,
bool syncLocal = false,
OnSuccessAll<T>? onSuccess,
OnErrorAll<T>? onError,
DataRequestLabel? label,
}) async {
params = await defaultParams & params;
headers = await defaultHeaders & headers;
label = DataRequestLabel('findAll', type: internalType, withParent: label);
late List<T> models;
if (shouldLoadRemoteAll(remote, params, headers) == false || background) {
models = findAllLocal();
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 clearLocal(notify: false);
}
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 Adapter<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 Adapter<T>);
},
);
if (background) {
(() async {
await future;
})();
return models;
} else {
return await future ?? <T>[];
}
}