0

I'm trying to get the near by location using firebase query and it goes well. This the function that gets the nearby location.

Future<List<DocumentSnapshot>> nearbyLocations() async {
    CollectionReference collectionRefer =
        FirebaseFirestore.instance.collection('locations');
    double radius = 10;
    String field = 'position';
    List<DocumentSnapshot> docList;
    GeoFirePoint center = await getCurrentLocation();
    // print(center);
    Stream<List<DocumentSnapshot>> stream = geo
        .collection(collectionRef: collectionRefer)
        .within(center: center, radius: radius, field: field, strictMode: true);
    stream.listen((List<DocumentSnapshot> documentList) {
      if (documentList.length > 0) {
        print(documentList[0].data());
        docList = documentList;
      } else {
        return {};
      }
    });
  }

I know that the query will return only one data. So, I printed on the first value in the above function. The problem arises when the documentList is returned.

loadData() async {
    documentList =
        await GeoLocator().nearbyLocations();
  }

When I call this above function, It prints null. But When I tried to print in the nearbyLocations() It print the data. But not when I Call loadData(). I'm going to use this returned data in listview.

2
  • This seems like an async issue. Your nearbyLocations() is an async method. Either you need to make you loadData() async or add a callback to nearbyLocations() using then((value){ // print value here}); Commented Oct 28, 2020 at 7:28
  • @BurhanuddinRashid I forgot to update my code. I also used async to loadData() and still after then It prints null. Commented Oct 28, 2020 at 7:33

1 Answer 1

1

You are mixing await, streams and then. This is probably too much to keep in mind at the same time.

Focus on one method at first. I suggest async/await since that is the easiest.

Your nearbyLocations method does not return anything. You did not define a type and it does not have a return statement either. Yet, you seem to expect it to return a Future<> with a specified type.

Make sure you crank up your warnings and use the pedantic package to have your analyzer notify you when you forget those things.

When you actually declare your method fully, your warnings should show you that you have no return in your method.


I don't have a compiler here or packages to include, but this seems to be what you really want:

Future<List<DocumentSnapshot>> nearbyLocations() async {
    final collectionRefer = FirebaseFirestore.instance.collection('locations');
    final radius = 10.0;
    final field = 'position';
    final center = await getCurrentLocation();

    final stream = geo
        .collection(collectionRef: collectionRefer)
        .within(center: center, radius: radius, field: field, strictMode: true);

    return stream.first;
  }
Sign up to request clarification or add additional context in comments.

7 Comments

As you said I made some alterations to my code but still the nearbyLocations() function returns null. Please help me sort this out!
Well, your nearbyLocations method still has no return statement. Your two return statements belong to the anonymous method used in the stream listener. Why is it a stream in the first place? Shouldn't it be a Future, because you only want a single result set?
If multiple shops are found near me all the shops should be returned so I used stream. I'll try adding return statements correctly
Do you want this to be a continuous stream while your location changes or do you want this to be a snapshot of the data at the current location?
Snapshot of the data at current location. As you said I placed a return statement in nearbyLocation function but no change in output.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.