I'm trying to move to flutter_hooks but I can't get some simple textformField validation to work.
I have 2 textfields and a button and I'd like to show an error on some conditions (or at least when the textfield is empty)
My code: -controllers:
final _emailFieldController =
useTextEditingController.fromValue(TextEditingValue.empty);
final _password =
useTextEditingController.fromValue(TextEditingValue.empty);
final _onSavePressed = useState(false);
-widgets:
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;
},
)
Thank you for your help!
Use
value
that you get from validator. for examplevalidator: (value) => value.isEmpty ? "Can't be empty" : null
gives you access to the value of the password field.