firebase 参数不匹配的闭包调用:函数“[]”

kx5bkwkv  于 2022-12-24  发布在  其他
关注(0)|答案(1)|浏览(488)

参数不匹配的闭包调用:函数"[]"接收方:闭包:()=〉从函数'data':Map〈String,dynamic〉。尝试调用:[]("文本")发现:[]()=〉Map〈字符串,动态〉
导致错误的相关小部件为
StreamBuilder〈查询快照〈对象?〉〉

class MessagesStream extends StatelessWidget {
  const MessagesStream({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Flexible(
      flex: 4,
      child: StreamBuilder<QuerySnapshot>(
        stream: _firestore
            .collection('messages')
            // Sort the messages by timestamp DESC because we want the newest messages on bottom.
            .orderBy("timestamp", descending: true)
            .snapshots(),
        builder: (context, snapshot) {
          // If we do not have data yet, show a progress indicator.
          if (!snapshot.hasData) {
            return const Center(
              child: CircularProgressIndicator(),
            );
          }
          // Create the list of message widgets.

          List<Widget> messageWidgets = snapshot.data!.docs.map<Widget>((m) {
            final data = m.data as dynamic;
            final messageText = data['text'];
            final messageSender = data['sender'];
            final currentUser = loggedInuser!.email;
            final timeStamp = data['timestamp'];
            return MessageBubble(
              sender: messageSender,
              text: messageText,
              timestamp: timeStamp,
              isMe: currentUser == messageSender,
            );
          }).toList();

          return Expanded(
            child: ListView(
              reverse: true,
              padding:
                  const EdgeInsets.symmetric(horizontal: 10.0, vertical: 20.0),
              children: messageWidgets,
            ),
          );
        },
      ),
    );
  }
}
eyh26e7m

eyh26e7m1#

更改此行:

final data = m.data as dynamic;

用这个:

final data = m.data() as Map<String, dynamic>;

调用m.data将返回函数的回调函数,该函数应该返回Map<String, dynamic>,而不是实际的Map〈String,dynamic〉。
你需要把它命名为m.data()

相关问题