我正在尝试开发一个消息传递应用程序,它总是抛出一个异常
'类型' _InternalLinkedHashMap<字符串,'>'不是'String'类型的子类型
当我试图给消息的背景颜色。它还显示
renderflex溢出99335像素
当在list中使用MessageBubble而不是list时。
final messages = snapshot.data.docs;
List<**MessageBubble**> messageBubbles = [];
for (var message in messages){
final messageText = message.data();
final messageSender = message.data();
final messageBubble = MessageBubble(sender: messageSender, text:
messageText,);
messageBubbles.add(messageBubble);
}
return Expanded(
child: ListView(
padding: EdgeInsets.symmetric(horizontal: 10,vertical: 20),
children : messageBubbles,
),
);
},
),
Container(
decoration: kMessageContainerDecoration,
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Expanded(
child: TextField(
onChanged: (value) {
messageText = value;
},
decoration: kMessageTextFieldDecoration,
),
),
FlatButton(
onPressed: () {
_fireStore.collection('messages').add({
'text': messageText,
'sender': loggedInUser.email,
}
);
},
child: Text(
'Send',
style: kSendButtonTextStyle,
),
),
],
),
),
],
),
),
);
}
}
class MessageBubble extends StatelessWidget {
MessageBubble({this.sender, this.text});
String sender;
String text;
@override
Widget build(BuildContext context) {
return Material(
color: Colors.blueAccent,
child: Text(
'$text from $sender',
style: TextStyle(
fontSize: 50,
color: Colors.white,
),
),
);;
}
}
这是因为根据您的模型,MessageBubble
需要两个字符串[sender
和text
]。但当你经过:
final messageText = message.data();
message.data()是一个Map<String,dynamic>
。这个地图中包含文本的字段是什么?例如,如果在Firebase中此字段称为textField
,则需要使用以下命令:
final messageText = message.get('textField');