Home > Software design >  Invalid constant value on Textformfield
Invalid constant value on Textformfield

Time:05-04

I try to do show and hide password but it says Invalid constant value on the _togglePasswordView, And _isHidden I wonder what I did wrong If someone know plz help

bool _isHidden = true;

 void _togglePasswordView() {
 setState(() {
  _isHidden = !_isHidden;
  });
}

TextFormField(
          obscureText: _isHidden,
          decoration: const InputDecoration(
            hintText: 'Password',
            suffix: InkWell(
              onTap: _togglePasswordView,
              child: Icon(
                _isHidden ? Icons.visibility : Icons.visibility_off,
              ),
            ),
          ),
        ),

CodePudding user response:

So lets make it clear about const keyword. When you use const keyword - you define a value constant from compile-time. It means - its value must be defined before the program even runs. So you may not but any run-time (created at some time when program is running) variables or conditions into constant constructors. Just remove the constant keyword.

CodePudding user response:

You're using something dynamic (here onTap function), inside a const constructor call

JUST REMOVE const KEYWORD and you'll be fine

  • Related