简体   繁体   中英

snapshot.data!.docs; not working, The error appears on the word docs

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),
            ),
          );
        }
      },
    );
  }
}

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM