In the line of docs, an error appears. Why does this error appear? Is there an error in writing the code?
I found this same question among the questions, but without an answer,
I’m still a beginner and I hope you can help me
Code:
class Messages extends StatelessWidget {
const Messages({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return StreamBuilder(
stream: FirebaseFirestore.instance
.collection('chat')
.orderBy('createdAt', descending: true)
.snapshots(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Stack(
children: [
Container(
width: double.infinity,
height: double.maxFinite,
color: Theme.of(context).colorScheme.surface,
),
const Center(
child: CircularProgressIndicator(),
),
],
);
} else {
final chatDocs = snapshot.data!.docs;
return ListView.builder(
//physics: const BouncingScrollPhysics(parent: AlwaysScrollableScrollPhysics()),
//padding: const EdgeInsets.symmetric(horizontal: 4),
reverse: true,
itemCount: chatDocs.length,
itemBuilder: (context, index) => MessageBubble(
chatDocs[index]['text'],
chatDocs[index]['userId'] ==
FirebaseAuth.instance.currentUser!.uid,
chatDocs[index]['username'],
chatDocs[index]['userImage'],
//key: ValueKey(chatDocs[index].id),
),
);
}
},
);
}
}
2
Answers
The solution was to add Query Snapshot to StreamBuilder, like this: StreamBuilder<QuerySnapshot>(
................. Thanks to everyone who tried to help
Try snapshots.data()
Firebase has different accessors for futures and streams