我正在使用StreamBuilder
将流转换为Widgets
,但是我在运算符中突出显示了字符串,并显示了错误:The operator '[]' isn't defined for the type 'Object? Function()
这是代码:
void messageStream() async {
await for (var snapshot in _firestore.collection('messages').snapshots()) {
for (var message in snapshot.docs) {
print(message.data());
}
}
}
body: SafeArea(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
StreamBuilder<QuerySnapshot>(
stream: _firestore.collection('messages').snapshots(),
builder: (context, snapshot) { //This is where the second error appeared
if (snapshot.hasData) {
final messages = snapshot.data?.docs;
List<Text> messageWidgets = [];
for (var message in messages!) {
final messageText = message.data['text']; //These are where the errors can be found
final messageSender = message.data['sender'];//These are where the errors can be found.
final messageWidget = Text('$messageText from $messageSender');
messageWidgets.add(messageWidget);
}
return Column(
children: messageWidgets,
);
}
return build(context);
},
),
1条答案
按热度按时间d4so4syb1#
.data
是一个 * 函数 *。因此,您必须使用括号()
来调用它。此外,您可能必须使用as
关键字来转换正确的类型。代替:
试试看:
或者:您可以使用
.get()
: