流订阅未侦听变化

时间:2020-05-26 02:54:08

标签: flutter google-cloud-firestore bloc flutter-bloc

我正在尝试以 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();
    });
  }

这是怎么了?

0 个答案:

没有答案