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.

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

prefix can be specified to prefix the path.

データが見つかった場合は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 value = data._readFromPath(paths, 0);
  if (value is! DynamicMap) {
    return null;
  }
  final limitValue = query.query.filters
      .firstWhereOrNull((e) => e.type == ModelQueryFilterType.limit)
      ?.value as int?;
  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(),
      )
      .sublist(0, limitValue);
  _collectionEntries[query] = List.from(entries);
  return Map<String, DynamicMap>.fromEntries(entries);
}