Im trying to fetch data from firebase and displaying the data into a listtile, but Im getting an error when trying to map the documents data into a list to display its contents
Here's my code
StreamBuilder<QuerySnapshot>(
stream: users.snapshots(),
builder: (BuildContext context,
AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError) {
return const Center(
child: Text("Something went wrong"),
);
}
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(
child: Text("Loading"),
);
}
if (snapshot.hasData) {
return CustomScrollView(
slivers: [
SliverList(
delegate: SliverChildListDelegate(
snapshot.data!.docs.map(
(DocumentSnapshot document) {
Map<String, dynamic> data = document.data()!;
return ListTile(
onTap: () => callChatDetailsScreen(
context, data['name'], data['uid']),
title: Text(data['name']),
subtitle: Text(data['status']),
);
},
).toList(),
),
)
],
);
}
return Container();
})
How can I fix this?
CodePudding user response:
Change this:
(DocumentSnapshot document) {
into this:
(DocumentSnapshot<Map<String,dynamic>> document) {
data()
returns a value of type T
and the class DocumenSnapshot
is declared in this way: abstract class DocumentSnapshot<T extends Object?>
, therefore if you don't specify a type for DocumentSnapshot
then data()
will return a value of type Object
.