Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding document changes variant for collection stream #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions packages/firestore_service/lib/firestore_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,37 @@ class FirestoreService {
});
}

Stream<List<T>> collectionStreamChanges<T>({
required String path,
required T Function(Map<String, dynamic>? data,
String documentID,
String changeType,) builder,
Query<Map<String, dynamic>>? Function(Query<Map<String, dynamic>> query)?
queryBuilder,
int Function(T lhs, T rhs)? sort,
}) {
Query<Map<String, dynamic>> query =
FirebaseFirestore.instance.collection(path);
if (queryBuilder != null) {
query = queryBuilder(query)!;
}
final Stream<QuerySnapshot<Map<String, dynamic>>> 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<T> documentStream<T>({
required String path,
required T Function(Map<String, dynamic>? data, String documentID) builder,
Expand Down