skip to Main Content

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

enter image description here

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


  1. Chosen as BEST ANSWER

    The solution was to add Query Snapshot to StreamBuilder, like this: StreamBuilder<‏QuerySnapshot>(

    ................. Thanks to everyone who tried to help


  2. Try snapshots.data()
    Firebase has different accessors for futures and streams

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search