gpt4 book ai didi

android - flutter : Custom TextFormField is not saving values onSaved

转载 作者:行者123 更新时间:2023-11-29 02:20:52 31 4
gpt4 key购买 nike

onSaved 没有在我调用这个类的主类中保存我的值

我想根据 api 返回数据生成自定义表单并将数据绑定(bind)到表单中,然后它应该按照在 flutter 中调用的验证方式进行验证

我已经创建了用于生成表单字段的自定义文本表单字段

在主类中,我使用带有 for 循环的 ListView 来生成一个列表,然后我的表单将被验证

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() 函数不会在验证成功后自动调用。我们必须手动调用 _formKey.currentState.save() 来保存我们的变量。祝你好运

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

关于android - flutter : Custom TextFormField is not saving values onSaved,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55806935/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com