load method
Retrieves data and updates the data in the model.
You will be notified of model updates at the time they are retrieved.
In addition,
the updated Resuult
can be obtained at the stage where the loading is finished.
Implementation
Future<void> load() async {
if (_loadCompleter != null) {
return loading;
}
_loadCompleter = Completer<void>();
try {
await onLoad();
bool notify = false;
_query ??= LocalStoreCollectionQuery(
path: path,
callback: _handledOnUpdate,
filter: parameters.isEmpty || !enableQueryFilter
? null
: (data) => ModelQuery._filter(parameters, data),
origin: this,
);
LocalDatabase._db.addCollectionListener(_query!);
final data = await LocalDatabase._db.loadCollection(_query!);
if (isNotEmpty) {
clear();
notify = true;
}
if (data.isNotEmpty) {
notify = true;
_rawEntries = ModelQuery._sort(
parameters,
List.from(data!.entries),
);
_rawValues.clear();
for (final tmp in _rawEntries) {
if (tmp.key.isEmpty) {
continue;
}
if (!filter(tmp.value)) {
continue;
}
final value = createDocument("${path.trimQuery()}/${tmp.key}");
value.value = value.fromMap(
value.filterOnLoad(Map<String, dynamic>.from(tmp.value)),
);
_rawValues.add(value);
}
final limit = ModelQuery.limitCountFrom(parameters);
addAll(
limit != null
? _rawValues.sublist(0, limit * _nextCount)
: _rawValues,
);
}
if (notify) {
notifyListeners();
}
await onDidLoad();
_loadCompleter?.complete();
_loadCompleter = null;
} catch (e) {
_loadCompleter?.completeError(e);
_loadCompleter = null;
} finally {
_loadCompleter?.complete();
_loadCompleter = null;
}
}