I am trying to get the data from the text field. While pressing the send button I need the data provided in text field to be stored up in '_message'. This is not working as expected thus my Map(newMessage) does not hold any value and thus my message list is empty. I need some solution so that my _message hold the value provided using OnFieldSubmitted.
Even while running the app it only prints (send 'value written in text box') and does not print the _message provided using OnFeildSubmitted.
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);
},
),
)
)
],
You need to implement this:
/// In `State` class body
String _message;
final _formKey = GlobalKey<FormState>();
/// In `build` method body.
Form(
key: _formKey, // <-- This is required.
child: TextFormField(
controller: _controller,
onSave: (value) {
_message = value;
}
),
),
IconButton(
icon: Icon(
Icons.send,
color: Colors.blue
),
onPressed: () {
if(_formKey.currentState.validate()) {
_formKey.currentState.save(); // This call triggers `onSave` callbacks.
_sendMesage(_message); // Handle _message variable.
}
},
,
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With