findStreamContainingList method

Stream<List<Map<String, dynamic>>> findStreamContainingList(
  1. dynamic collectionName,
  2. dynamic docId,
  3. dynamic listIds
)

Implementation

Stream<List<Map<String, dynamic>>> findStreamContainingList(
    collectionName, docId, listIds) async* {
  StreamController<List<Map<String, dynamic>>> controller =
      StreamController();

  List<Map<String, dynamic>> result = [];

  for (String documentId in listIds) {
    try {
      DocumentSnapshot documentSnapshot =
          await _firestore.collection(collectionName).doc(documentId).get();

      if (documentSnapshot.exists) {
        Map<String, dynamic> data =
            documentSnapshot.data() as Map<String, dynamic>;
        result.add(data);
      } else {
        if (kDebugMode) {
          print('Document $documentId does not exist.');
        }
      }
    } catch (e) {
      if (kDebugMode) {
        print('Error fetching document $documentId: $e');
      }
    }
  }

  // Add the result to the stream
  controller.add(result);

  // Close the stream
  controller.close();

  // Yield the stream
  yield* controller.stream;
}