loadCollection method

Future<Map<String, DynamicMap>?> loadCollection(
  1. ModelAdapterCollectionQuery query, {
  2. String? prefix,
})

Pass query and load the collection corresponding to query.

If data is found, it will be returned in Map<String, DynamicMap>.

String contains the ID of each document (if the path is aaaaa/bbbbb/cccc/ddddd, the ID is ddddd).

If no data is found or the path is invalid, Null is returned.

prefix can be specified to prefix the path.

queryを渡してqueryに対応するコレクションを読み込みます。

データが見つかった場合はMap<String, DynamicMap>で返されます。

StringにはそれぞれのドキュメントのID(パスがaaaa/bbbb/cccc/ddddの場合IDはddddになります)が含まれています。

データが見つからなかったり、パスに不正があった場合はNullが返されます。

prefixを指定するとパスにプレフィックスを付与可能です。

Implementation

Future<Map<String, DynamicMap>?> loadCollection(
  ModelAdapterCollectionQuery query, {
  String? prefix,
}) async {
  _addCollectionListener(query, prefix: prefix);
  await _initialize();
  await onLoad?.call(this);
  final trimPath = _path(query.query.path, prefix);
  final paths = trimPath.split("/");
  if (paths.isEmpty) {
    return null;
  }
  final isCollectionGroup = query.query.isCollectionGroup;
  final value = isCollectionGroup
      ? data._readFromGroup(trimPath.last(), 0)
      : data._readFromPath(paths, 0);
  if (value is! DynamicMap) {
    return null;
  }
  final limitValue = _limitValue(query);
  final entries = query.query.sort(
    value
        .toList(
          (key, value) {
            if (value is! Map) {
              return null;
            }
            return MapEntry(
              key,
              Map<String, dynamic>.from(value),
            );
          },
        )
        .where(
          (element) =>
              element != null && query.query.hasMatchAsMap(element.value),
        )
        .removeEmpty(),
  );
  final limited = entries.sublist(
    0,
    limitValue != null ? min(limitValue, entries.length) : null,
  );
  if (isCollectionGroup) {
    _collectionGroupEntries[query.origin] = List.from(limited);
  } else {
    _collectionEntries[query.origin] = List.from(limited);
  }
  return Map<String, DynamicMap>.fromEntries(limited);
}