简体   繁体   English

如何使用 Flutter TextFormField 设置状态()

[英]how to setState() using Flutter TextFormField

validator: (value) {
                                if (value.isEmpty) {
                                  _casing.depth = 0;
                                } else if (double.tryParse(value) != null) {
                                  // setState(() {
                                  _casing.depth = double.tryParse(value);
                                  // });
                                } else {
                                  _casing.depth = 0;
                                  return kTextFieldError;
                                }
                                return null;
                              },

This returns an error that says unable to setState while currently doing so.这将返回一个错误,指出当前执行此操作时无法设置状态。 What I need is as the user inputs (or finishes either by clicking ok or tapping anywhere which automatically hides keyboard), I want to use the input value to calculate numbers in the parent widget我需要的是作为用户输入(或通过单击确定或点击自动隐藏键盘的任何位置完成),我想使用输入值来计算父小部件中的数字

you can use the onChanged property of TextFormField您可以使用TextFormFieldonChanged属性

onChanged:(value){
 if (value.isEmpty) {
_casing.depth = 0;
} else if (double.tryParse(value) != null) {
 // setState(() {
 _casing.depth = double.tryParse(value);
 // });
 } else {
  _casing.depth = 0;
   return kTextFieldError;
 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter - 如何使用 Navigator 在 TextFormField 中获取值 - Flutter - How to get value in TextFormField using Navigator Flutter - 如何使用切换按钮启用 TextFormField - Flutter - How to enable TextFormField using Switch button 在 TextFormField Flutter 中使用 FocusNode - Using FocusNode in TextFormField Flutter Flutter Textformfield 如何在 Textformfield 中居中文本 - Flutter Textformfield How to center text in a Textformfield 如何使用 flutter 中的 CheckBox 将数据从一个 TextFormField 复制到另一个 TextFormField - How to copy data from one TextFormField to Another TextFormField by using CheckBox in flutter Flutter根据一个widget变量的setState设置TextFormField的值 - Flutter setting a value of TextFormField according to setState of a widget variable Flutter:TextFormField:Cursor 在调用 setState() 后重置为开始 - Flutter: TextFormField: Cursor resets to beginning after calling setState() Flutter TextField/TextFormField 不会在 setState() 上更新其值 - Flutter TextField/TextFormField does not update its value on setState() 如何使用 Flutter 中的格式化程序格式化 TextFormField 的初始值? - How to format initial value for TextFormField using formatter in Flutter? 当状态改变时如何使用 Provider/ChangeNotifier 使 TextFormField 颤动? - How to TextFormField in flutter when state changes, using Provider/ChangeNotifier?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM