I want to show users within a certain area but using geo query I am getting an error
Class 'List<DocumentSnapshot<Object?>>' has no instance getter 'docs'.
Receiver: Instance(length:1) of '_GrowableList'
Tried calling: docs
Expanded (
child:StreamBuilder (
stream: geo.collection(collectionRef: FirebaseFirestore.instance.collection("users"))
.within(
radius: radius, field: field, center: geo.point(
latitude: Latitude,
longitude: Longitude
), ),
builder: (BuildContext context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(
child: CircularProgressIndicator(),
);
}
return ListView.builder(
scrollDirection: Axis.vertical,
shrinkWrap: false,
itemCount: snapshot.data.length,
itemBuilder: (context, index) =>
UserCard(
data: (snapshot.data as dynamic).docs[index].data(),
),);
},
)
),
CodePudding user response:
you are doing a slight mistake that the geo stream provides a list of DocumentSnapshot object not a QuerySnapshot object as CloudFirestore package does.
So, you don't have to call snapshot.data.docs, actually snapshot.data is exactly the docs collection.
You can just get the DocumentSnapshot at index by the following code:
return ListView.builder(
scrollDirection: Axis.vertical,
shrinkWrap: false,
itemCount: snapshot.data.length,
itemBuilder: (context, index) => UserCard(
data: (snapshot.data as dynamic)[index].data(),
),
);
CodePudding user response:
This is the format-
StreamBuilder(
stream: FirebaseFirestore.instance.collection(collectionsnapshot).snapshots(),
builder: (context,
AsyncSnapshot<QuerySnapshot<Map<String, dynamic>>> snapshot) {
//And to reference the docs, do snpashot.data!.docs
},
),
Specifically for you, try-
Expanded (
child:StreamBuilder (
stream: geo.collection(collectionRef: FirebaseFirestore.instance.collection("users").shapshots())
.within(
radius: radius, field: field, center: geo.point(
latitude: Latitude,
longitude: Longitude
), ),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot<Map<String, dynamic>>>) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(
child: CircularProgressIndicator(),
);
}
return ListView.builder(
scrollDirection: Axis.vertical,
shrinkWrap: false,
itemCount: snapshot.data.length,
itemBuilder: (context, index) =>
UserCard(
data: (snapshot.data as dynamic).docs[index].data(),
),);
},
)
),
I'm not too sure how things work with Geo collection, but try this and tell me what comes up.
First add .snapshots() to FirebaseFirestore.instance.collection("users") and hover user .snapshots() to see what it returns.
