Flutter : Custom TextFormField is not saving values onSaved
4,567
onSaved() function won't be called automatically after successful validation. We have to call _formKey.currentState.save() manually to save our variables. Good luck
Form(
key: key,
child: TextFormField(
onSaved: (val) {
print('saved');
},
validator: (val) {
print('validating');
},
),
),
RaisedButton(
child: Text('Click me'),
onPressed: () {
if (key.currentState.validate()) {
key.currentState.save();
print('valid');
}
},
),
Author by
Amit
Mobile Application Developer in Darshan Soft-Tech Pvt. Ltd Coffee ,a laptop and a dream are enough to conquer the world!
Updated on December 10, 2022Comments
-
Amit over 1 year
onSaved is not saving my values in the main class from where i am calling this class
i want to generate custom form as per api return data and bind the data in form and then it should validate as called on validate in flutter
i have created custom textform field for generating form field
from main class i am using listview with for loop to generate a list and then my form is going to validate
class CustomTextField extends StatefulWidget { CustomTextField( {@required this.focusNode, @required this.nextFocusNode, @required this.textEditingController, @required this.validator, @required this.labelText, @required this.dataText}); final FocusNode focusNode; final FocusNode nextFocusNode; final TextEditingController textEditingController; final FormFieldValidator<String> validator; final String labelText; String dataText; @override _CustomTextFieldState createState() => new _CustomTextFieldState(); } class _CustomTextFieldState extends State<CustomTextField> { @override Widget build(BuildContext context) { return Container( height: 65, child: new TextFormField( style: Utility.textFormFieldStyle(context), keyboardType: TextInputType.text, textInputAction: widget.nextFocusNode == null ? TextInputAction.done : TextInputAction.next, focusNode: widget.focusNode, onFieldSubmitted: (v) { FocusScope.of(context).requestFocus(widget.nextFocusNode); }, decoration: InputDecoration( labelText: widget.labelText, contentPadding: Utility.edgeInsetsGeometry()), controller: widget.textEditingController, validator: widget.validator, onSaved: (String val) { widget.dataText = val; // not saving my value in my main class print("costom text view ${widget.dataText}"); })); } } /// main class ----- belo code is run from a stateful class ----------------------/// List<FocusNode> listFocusNode; List<String> listDataText; List<TextEditingController> listTextEditingController; List<Widget> listFormField; @override void initState() { super.initState(); listFocusNode = <FocusNode>[]; listTextEditingController = <TextEditingController>[]; listFormField = <Widget>[]; listDataText = <String>[]; for (int i = 0; i < 5; i++) { listFocusNode.add(FocusNode()); } for (int i = 0; i < 5; i++) { listDataText.add("old"); } for (int i = 0; i < 5; i++) { listTextEditingController.add(TextEditingController()); } for (int i = 0; i < 5; i++) { listFormField.add(CustomTextField( dataText: listDataText[i], focusNode: listFocusNode[i], labelText: "field$i", textEditingController: listTextEditingController[i], nextFocusNode: i == 4 ? null : listFocusNode[i + 1], // validator: validateNull, validator: validateName, )); } } _validateForm() { if (_key.currentState.validate()) { // No any error in validation _key.currentState.save(); print("Name ${listDataText.toString()}"); // when i print this data in log its printing old data only but onSaved should save value to the proper location in array list Navigator.of(context).pop(); } else { setState(() { _validate = true; }); }
}
-
Amit about 5 years_validateForm() { if (_key.currentState.validate()) { // No any error in validation _key.currentState.save(); print("Name ${listDataText.toString()}"); // when i print this data in log its printing old data only but onSaved should save value to the proper location in array list Navigator.of(context).pop(); } else { setState(() { _validate = true; }); } }
-
Amit about 5 yearsi already called key.currentState.save(); but it is not saving my values
-
GirlWhoCode about 5 yearsOkey , you can use this if (key.currentState.validate()) { key.currentState.save(); value = controller.text }
-
th_lo about 3 yearsdo you know when onFieldSubmitted is called?
-
GirlWhoCode about 3 yearswhen u click submit on the keyboard on the right bottom !!! when u specify InputTextAction to DONE !!