I am trying to get nearby pharmacies using a GeoFlutterFire query and display them, but there are no elements in the stream after running the function.
This is the function I used to get the stream of nearby pharmacies.
Stream<List<DocumentSnapshot>> getNearbyPharmacies(LatLng userPosition, String medication) {
final geo = GeoFlutterFire();
GeoFirePoint center = geo.point(latitude: userPosition.latitude, longitude: userPosition.longitude);
var collectionReference = firestore.collection('Pharmacies');
var radius = 5.0; // radius in kilometers
String field = 'Position';
Stream<List<DocumentSnapshot>> stream = geo.collection(collectionRef: collectionReference)
.within(center: center, radius: radius, field: field);
return stream;
}
And this is the code I used to check whether there are elements to display.
StreamBuilder<List<DocumentSnapshot>>(
stream: _pharmacyStream,
builder: (context, snapshot) {
if (snapshot.hasError) {
return Text('Error: ${snapshot.error}');
}
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(child: CircularProgressIndicator());
}
if (!snapshot.hasData || snapshot.data == null) {
return Text('No data available');
} else {
var docs = snapshot.data!;
return Expanded(
child: Column(
children: [
Expanded(
child: ListView.builder(
//...
),
),
],
),
);
}
},
)
I have already created the documents with necessary fields including a map type filed called ‘Position’, inside a collection called ‘Pharmacies’.
New contributor
J_Max is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.