Password & Password Confirm in Flutter Form

Question

Asked by Learning C on November 12, 2021 (source).

I have a form with 2 password inputs , one for password and the second to confirm it. I used passwordController to get the first typed password and value as arguments in my function to get the confirmed typed password

I was trying to do it like that :

final _passwordController = TextEditingController();

final _confirmpasswordController = TextEditingController();

String passwordInputValidator(TextEditingController _passwordController, value) {
if (value =!  _passwordController.text) {
return 'Password doesnt match';
}
}

MakeInput('Type your password', true,
                        nameInputValidator, _passwordController)),
               
                    MakeInput(
                        'Confirm Password',
                        true,
                        passwordInputValidator,
                        _confirmpasswordController)),

Widget MakeInput(label, obscureText, validator, controller) {
return Column(
  crossAxisAlignment: CrossAxisAlignment.start,
  children: <Widget>[
    Text(
      label,
      style: TextStyle(
          fontSize: 15, fontWeight: FontWeight.w400, color: Colors.white),
    ),
    SizedBox(
      height: 5,
    ),
    TextFormField(
        obscureText: obscureText,
        controller: controller,
        style: TextStyle(color: Colors.white),
        decoration: InputDecoration(
            contentPadding:
                EdgeInsets.symmetric(vertical: 0, horizontal: 12),
            enabledBorder: OutlineInputBorder(
                borderSide: BorderSide(color: Colors.purple[800])),
            border: OutlineInputBorder(
                borderSide: BorderSide(color: Colors.purple[800])),
            focusedBorder: OutlineInputBorder(
              borderSide: BorderSide(color: Colors.purple[800], width: 2.0),
            )),
        validator: validator),

I got this error now and I don't know how to solve it.

_TypeError (type '(TextEditingController, dynamic) => String' is not a subtype of type 
 '((String?) => String?)?')

The error occured on this line of my widget :

validator: validator

Answer

Question answered by Alberto A (source).

one of the problems there is that u wrote the relational operator (different) in reverse (=!) and it shoud be (!=). And I think you could use both controllers to validate the input (because they are global), like this.

String? passwordInputValidator(value) {
    if (value == null || _confirmpasswordController !=  _passwordController.text) {
        return 'Password doesnt match';
    return null;
    }
}

I think this way it will work;

DART FLUTTER
SHARE: