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> 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>(
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(
itemCount: docs.length,
itemBuilder: (context, index) {
return Column(
children: [
GestureDetector(
onTap: () {
Navigator.pushNamed(context, ‘/pharmacydetails’, arguments: docs[index].data());
},
child: Container(
margin: const EdgeInsets.only(left: 10.0, right: 10.0),
width: MediaQuery.of(context).size.width,
decoration: const BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.all(Radius.circular(10)),
boxShadow: [
BoxShadow(
color: Color(0x40FFFFFF),
blurRadius: 4.0,
offset: Offset(0, 4),
),
],
),
child: Padding(
padding: const EdgeInsets.only(top: 16.0, bottom: 5.0, left: 14.0, right: 14.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
docs[index][‘Name’],
style: TextStyle(
fontSize: 20.0,
),
),
Container(
width: 47.0,
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Text(
“4.6”,
style: TextStyle(
fontSize: 15.0,
),
),
Icon(
Icons.star,
color: Colors.amber,
size: 24.0,
),
],
),
),
],
),
SizedBox(
height: 20.0,
),
Text(
docs[index][‘ContactNo’],
style: TextStyle(
fontSize: 20.0,
),
),
],
),
),
),
),
SizedBox(
height: 7.0,
),
],
);
},
),
),
],
),
);
}
},
)
I have already created the documents with necessary fields including a map type filed called ‘Position’, inside a collection called ‘Pharmacies’.
J_Max is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.