使用颤动钩子验证TextFormField

问题描述

我正在尝试转到flutter_hooks,但是我无法使用一些简单的textformField验证来工作。

我有2个文本字段和一个按钮,我想在某些情况下(或至少在文本字段为空时)显示错误

我的代码: -控制器:

final _emailFieldController =
        useTextEditingController.fromValue(TextEditingValue.empty);

    final _password =
        useTextEditingController.fromValue(TextEditingValue.empty);
    final _onSavePressed = useState(false);

-小部件:

TextFormField(
                  decoration: InputDecoration(hintText: "Your email"),controller: _emailFieldController,),TextFormField(
                  obscureText: true,onSaved: (newValue) => print(newValue),validator: (value) =>
                      _onSavePressed.value && _password.text.isEmpty
                          ? "Can't be empty"
                          : null,decoration: InputDecoration(
                    hintText: "Password",controller: _password,RaisedButton(
                  child: Text("Create"),onPressed: () {
                    _onSavePressed.value = true;
                  },)

谢谢您的帮助!

解决方法

使用从验证者那里获得的value。例如

validator: (value) => value.isEmpty ? "Can't be empty" : null

允许您访问密码字段的值。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...