【发布时间】:2020-02-03 19:08:59
【问题描述】:
我正在尝试从文本字段中获取数据。按下发送按钮时,我需要将文本字段中提供的数据存储在“_message”中。这没有按预期工作,因此我的 Map(newMessage) 不包含任何值,因此我的消息列表为空。 我需要一些解决方案,以便我的 _message 保存使用 OnFieldSubmitted 提供的值。
即使在运行应用程序时,它也只会打印(发送“文本框中写入的值”),而不会打印使用 OnFeildSubmitted 提供的 _message。
Padding(
padding: EdgeInsets.all(12.0),
child: Row(
children: <Widget>[
Expanded(
child: TextFormField(
controller: _controller,
onFieldSubmitted: (String _message){
print("on submit "+_message);
Map<String,dynamic> newMessage = {
"type": "string",
"content": _message,
"from": "me",
};
List<dynamic> newList = _listOfMessages;
newList.add(newMessage);
setState(() {
_listOfMessages = newList;
});
_controller.clear();
},
decoration: InputDecoration(
hintText: "Type your message here",
),
)),
Padding(
padding: EdgeInsets.all(8.0),
child: Center(
child: IconButton(
icon: Icon(Icons.send,
color: Colors.blue),
onPressed: () {
print("send " + _controller.text);
},
),
)
)
],
【问题讨论】:
-
如果 onPressed 代码正常工作,为什么不把 onFieldSubmitted 代码也移到那里?你想用 onFieldSubmitted 实现什么? PS。我没有记下你。
-
@GrahamD 很抱歉,因为我是这个颤振开发的新手。感谢您的帮助,它确实有效。
-
不用担心。我们都在学习。