I m trying to fetch Firestore
documents in BLoC pattern. There are two events, one fetches all the documents, the other fetches documents based on a query. Here is the code for the two events:
Stream<JobFeedState> _mapFetchAllJobPostsToState(
FetchAllJobPosts event) async* {
yield LoadingJobPosts();
try {
_jobsSubscription?.cancel();
_jobsSubscription = _jobsRepository.getAllTheJobsPosted().listen((jobs) {
add(JobPostsRefreshed(jobs: jobs));
});
} catch (e) {
yield Error(message: e.toString());
}
}
Stream<JobFeedState> _mapFetchJobPostsForLocation(
FetchJobPostsForLocation event) async* {
yield LoadingJobPosts();
try {
_jobsSubscription?.cancel();
_jobsInLocationsubscription?.cancel();
_jobsInLocationsubscription =
_jobsRepository.getJobsInALocation(event.location.country).listen((jobs) {
add(JobPostsRefreshed(jobs: jobs));
});
} catch (e) {
yield Error(message: e.toString());
}
}
_mapFetchAllJobPostsToState()
is working as expected. But the other one, _mapFetchJobPostsForLocation()
fetches data first time, but then it doesn't show the changes in stream. Here is the function that fetches data based on a query :
// getJobsInALocation()
Stream<List<JobPostDocumentModel>> getJobsInALocation(String location) {
var collectionGroup =
Firestore.instance.collectionGroup(AppStringConstants.posts);
return collectionGroup
.where(AppStringConstants.country, isEqualTo: location)
.snapshots()
.map((snapshot) {
return snapshot.documents.map((docSnapshot) {
DocumentReference documentReference = docSnapshot.reference;
JobPostModel jobPostModel = JobPostModel.fromSnapshot(docSnapshot);
JobPostDocumentModel jobPostDocumentModel = JobPostDocumentModel(
documentReference: documentReference,
jobPostModel: jobPostModel,
);
return jobPostDocumentModel;
}).toList();
});
}
Whats wrong here?