2021年4月24日星期六

How to use a stream on Firebase firestore colletions to show nearby collections by location inside a radius of the device using Flutter

I am developing an App using Flutter, like a food delivery app. So I have the restaurant app, and the end-user app.

What I am doing at the moment

Getting the company address (geoLocation), and using lat and lng with geohash to store the location on the Firestore collection for the Restaurants. And for the end-user app, I am getting the current device location.

What I am trying to achieve

On the end-user app, I want to create a list, like uber eats, to show the nearby restaurants, using the current location of the end-user device, with the Restaurant location on our Firestore.

I am using GeoFlutterFire dependencie to achieve the locations.

The function I have

Stream nearbyComp() async* {      var pos = await location.getLocation();        GeoFirePoint point =          geo.point(latitude: pos.latitude, longitude: pos.longitude);      final CollectionReference users =          _firebaseFirestore.collection("Companies");        double radius = 10;      String field = 'location';        Stream<List<DocumentSnapshot>> stream = geo          .collection(collectionRef: users)          .within(center: point, radius: radius, field: field, strictMode: true);        yield stream;    }  

Where I am trying to display the List

Container(              child: StreamBuilder(                stream: nearbyComp(),                builder: (context, snapshot) {                  if (!snapshot.hasData) {                    return Text("Loading");                  }                  return Container(                    child: Text("companyName"), // This needs to be changed to a list? Display company name, image, etc, here.                  );                },              ),            ),  

Current issue

The issue is, I can't find a way to display the Restaurant Profile on the end-user screen, like image, name, etc. I think I need a ListView.Builder, but don't know how to do it with the Stream on the nearComp() function.

What I have tried:

return ListView(children: [  for (var item in snapshot.data)     ListTile( title: Text ( item["companyEmail"]))   // Or use item.data()["companyEmail"]  ])  

But it returns me this error:

type '_AsBroadcastStream<List<DocumentSnapshot>>' is not a subtype of type 'Iterable<dynamic>'  

My Firestore

enter image description here

enter image description here

https://stackoverflow.com/questions/67249105/how-to-use-a-stream-on-firebase-firestore-colletions-to-show-nearby-collections April 25, 2021 at 10:06AM

没有评论:

发表评论