getFromIdList method
Gets a model list from an id list
This function verifies what models are in repositoryMap. Those that are not
in repositoryMap are obtained from the database.
The function splits the list in mini list of FirestoreConstants.ARRAY_QUERIES_ITEM_LIMIT
of size and use _getFromIdList()
for get the models
- Set
cache
to true to get the model from the cache of the database - Set
refreshRepoData
to always get the data from the database and rewrite it into repositoryMap - Set
notify
to call_update()
function and notify to all listener that is a change in repositoryMap in all models that are obtained from the database.
Implementation
Future<List<Model>> getFromIdList(
List<Id> idList, {
bool cache = false,
bool refreshRepoData = false,
bool notify = false,
}) async {
List<Id> _idList = [];
List<Model> res = [];
if (!refreshRepoData) {
Model? modelRes;
for (Id id in idList) {
modelRes = repositoryMap[id];
if (modelRes != null)
res.add(modelRes);
else
_idList.add(id);
}
} else
_idList = idList;
List<Model> newModels = [];
if (_idList.length <= PauloniaRepoConstants.ARRAY_QUERIES_ITEM_LIMIT) {
newModels.addAll((await _getFromIdList(_idList, cache: cache)) ?? []);
addInRepository(newModels);
if (_idList.isNotEmpty && notify)
update(RepoUpdateType.get, ids: _idList);
res.addAll(newModels);
return res;
}
int start = 0;
int end = PauloniaRepoConstants.ARRAY_QUERIES_ITEM_LIMIT;
while (true) {
if (end > _idList.length) end = _idList.length;
if (end == start) break;
newModels.addAll((await _getFromIdList(
_idList.getRange(start, end).toList(),
cache: cache)) ??
[]);
start = end;
end += PauloniaRepoConstants.ARRAY_QUERIES_ITEM_LIMIT;
}
addInRepository(newModels);
if (_idList.isNotEmpty && notify) update(RepoUpdateType.get, ids: _idList);
res.addAll(newModels);
return res;
}