我正在尝试以BLoC模式获取Firestore
文档。有两个事件,一个获取所有文档,另一个根据查询获取文档。这是两个事件的代码:
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()
正在按预期工作。但另一个,_mapFetchJobPostsForLocation()
第一次获取数据,但它没有显示流中的变化。这是基于查询获取数据的函数:
// 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();
});
}
这里有什么问题?