3

I have to query the geoflutterfire data with a Stream. But I would like to use this in a Future in a ChangeNotifierprovider to be able to wait until the Stream has all data.

I never built a Future with a Stream in it before. What do I have to return? When I don't have a return and just do notifyListeners(), the function runs forever. So I just put in return true.

Is there a better way to do this?

Future<void> fetchPlacesNearBy(double lat, double lng) async {
    print("GETTING FIRESTORE PLACES");

    GeoFirePoint center = geo.point(latitude: lat, longitude: lng);
    double radius = 5; //in km
    String field = 'geo_location';

    // Reading nearby places based on lat, lng parameters
    try {
      Stream<List<DocumentSnapshot>> placesStream =
          geo.collection(collectionRef: _placesRef).within(
                center: center,
                radius: radius,
                field: field,
                strictMode: false,
              ); // = false includes borderline places
      
      await for (var doclist in placesStream) {
        topWikiPlacesAroundUser2 = [];
        if (doclist.length > 0) {
          for (DocumentSnapshot ds in doclist) {
            /// Init place model
            WunderkPlace pl = WunderkPlace.fromMap(
                ds.data(), ds.get('geo_location.geopoint'));
            topWikiPlacesAroundUser2.add(pl);

          }
        } else {
          print('Nearby places do not exist in the database');
        }

        notifyListeners();
        return true;
      }
    } catch (e) {
      print(e);
      return false;
    }
  }
JoergP
  • 1,349
  • 2
  • 13
  • 28

0 Answers0