diff --git a/packages/firestore_service/lib/firestore_service.dart b/packages/firestore_service/lib/firestore_service.dart index f7bd83c..a1b66c7 100644 --- a/packages/firestore_service/lib/firestore_service.dart +++ b/packages/firestore_service/lib/firestore_service.dart @@ -48,6 +48,37 @@ class FirestoreService { }); } + Stream> collectionStreamChanges({ + required String path, + required T Function(Map? data, + String documentID, + String changeType,) builder, + Query>? Function(Query> query)? + queryBuilder, + int Function(T lhs, T rhs)? sort, + }) { + Query> query = + FirebaseFirestore.instance.collection(path); + if (queryBuilder != null) { + query = queryBuilder(query)!; + } + final Stream>> snapshots = + query.snapshots(); + return snapshots.map((snapshot) { + final result = snapshot.docChanges + .map((snapshot) => builder( + snapshot.doc.data(), + snapshot.doc.id, + snapshot.type.name,),) + .where((value) => value != null) + .toList(); + if (sort != null) { + result.sort(sort); + } + return result; + }); + } + Stream documentStream({ required String path, required T Function(Map? data, String documentID) builder,