首页 > 解决方案 > 在 Flutter 中传递和比较控制器

问题描述

我有一个带有 2 个密码输入的表格,一个用于密码,第二个用于确认。

我试图这样做:

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),

此验证器是确认密码的验证器TextField。所以我尝试将第一个密码输入值作为参数传递给我的验证器,并将其与我value确认的密码进行比较。

我在我的函数中遇到了这个错误:A negation operand must have a static type of 'bool'. Try changing the operand to the '!' 我假设这个错误是因为参数的动态类型,但是如何修复这个错误或者即使有任何逻辑错误。

标签: flutterdart

解决方案


您传递了错误的比较运算符 =!使用 != 代替

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

推荐阅读