0

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?

Newaj
  • 3,992
  • 4
  • 32
  • 50
  • *"_mapFetchJobPostsForLocation() fetches data first time, but then it doesn't show the changes in stream"* - what does it mean? tried to `print(jobs)` before calling `add(JobPostsRefreshed(jobs: jobs));`? – pskink May 26 '20 at 03:58
  • @pskink It means , I get expected result when that function is called first time. If there is any change in data, the changes should be visible in UI. But it is not. Infact the streamsubscription is not listening to the changes. – Newaj May 26 '20 at 04:34
  • `print(jobs)` first time prints **[Instance of 'JobPostDocumentModel']**. Then it doesn't print anything when the data changes. – Newaj May 26 '20 at 04:36
  • so where are you calling it? are you sure that you dont call `_jobsInLocationsubscription?.cancel();` somewhere? – pskink May 26 '20 at 04:36
  • If I use `_jobsRepository.getAllTheJobsPosted()`, I get expected result. Thats why I think problem is in `getJobsInALocation()` function. – Newaj May 26 '20 at 04:39
  • @pskink I dont call `_jobsInLocationsubscription?.cancel();` anywhere else. – Newaj May 26 '20 at 04:41
  • *" I get expected result when that function is called first time"* - so where and how many times do you call that function? i mean `_mapFetchJobPostsForLocation` – pskink May 26 '20 at 04:47
  • Actually I have two events for those two functions. In **BLoC** class there is another `StreamSubscription` for checking GPS. If GPS is on, an event calls `_mapFetchJobPostsForLocation`. If GPS is off, another event calls the other function. – Newaj May 26 '20 at 05:19

0 Answers0