loadDataWithQuery<T extends BaseEntityDataModel> method

Future<List<T>> loadDataWithQuery<T extends BaseEntityDataModel>({
  1. required String path,
  2. required T builder(
    1. Map<String, dynamic>? jsondata,
    2. String docId
    ),
  3. Query<Object?> queryBuilder(
    1. Query<Object?> query
    )?,
  4. int sort(
    1. T lhs,
    2. T rhs
    )?,
})

Loads data with a query from the specified path and returns it as a Future<List<T>>.

path - The path to the data. builder - The function to build the data. queryBuilder - The function to build the query. sort - The function to sort the data.

Implementation

Future<List<T>> loadDataWithQuery<T extends BaseEntityDataModel>({
  required String path,
  required T Function(Map<String, dynamic>? jsondata, String docId) builder,
  Query Function(Query query)? queryBuilder,
  int Function(T lhs, T rhs)? sort,
}) {
  Query query = _fireStore.collection(path);
  if (queryBuilder != null) {
    query = queryBuilder(query);
  }
  final snapshots = query.get();
  return snapshots.then((snapshot) {
    final result = snapshot.docs
        .map(
          (snapshot) => builder(
            snapshot.data() as Map<String, dynamic>,
            snapshot.id,
          ),
        )
        .toList();
    if (sort != null) {
      result.sort(sort);
    }
    return result;
  });
}