简体   繁体   中英

Flutter : Custom TextFormField is not saving values onSaved

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;
  });
}

}

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');
    }
  },
),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM