参数不匹配的闭包调用:函数"[]"接收方:闭包:()=〉从函数'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,
),
);
},
),
);
}
}
1条答案
按热度按时间eyh26e7m1#
更改此行:
用这个:
调用
m.data
将返回函数的回调函数,该函数应该返回Map<String, dynamic>
,而不是实际的Map〈String,dynamic〉。你需要把它命名为
m.data()
。