streamByQuery method

Stream<List<Map<String, dynamic>>> streamByQuery({
  1. required CollectionReferenceDef<Map<String, dynamic>>? collectionReferenceQuery,
  2. required String whereDescription,
})

Streams documents matching a query

Returns real-time updates for filtered documents Provides raw Firestore data without conversion

Parameters: collectionReferenceQuery custom query to filter documents whereDescription description of the query for logging

Returns Stream of List containing:

  • Document data
  • Real-time updates

Features:

  • Custom query support
  • Real-time updates
  • Raw data access
  • Error logging

Example:

final stream = api.streamByQuery(
  collectionReferenceQuery: (ref) => ref.where('age', isGreaterThan: 18),
  whereDescription: 'Adult users',
);
stream.listen((users) {
  print('Got ${users.length} adult users');
});

See also: streamByQueryWithConverter type-safe query streaming streamAll unfiltered streaming

Implementation

Stream<List<Map<String, dynamic>>> streamByQuery({
  required CollectionReferenceDef<Map<String, dynamic>>?
      collectionReferenceQuery,
  required String whereDescription,
}) {
  final path = _collectionPath();
  _log.debug(
    message: 'Finding stream by query..',
    sensitiveData: SensitiveData(
      path: path,
      whereDescription: whereDescription,
    ),
  );
  final query = collectionReferenceQuery?.call(listCollectionReference()) ??
      listCollectionReference();
  return query
      .snapshots()
      .map(
        (event) => event.docs.map((e) => e.data()).toList(),
      )
      .handleError(
    (error, stackTrace) {
      _log.error(
        message: 'Error streaming collection by query',
        sensitiveData: SensitiveData(
          path: path,
          whereDescription: whereDescription,
        ),
        error: error,
        stackTrace: stackTrace,
      );
      throw TurboFirestoreException.fromFirestoreException(
        error,
        stackTrace,
        path: path,
        query: whereDescription,
      );
    },
  );
}