【问题标题】:the validation of my TextFormField always throw an error我的 TextFormField 的验证总是抛出一个错误
【发布时间】:2021-09-24 05:52:41
【问题描述】:
child: TextFormField(
                                controller: emailcontroller,
                                decoration: InputDecoration(
                                    border: InputBorder.none,
                                    hintText: "Email or Phone number",
                                    hintStyle:
                                        TextStyle(color: Colors.grey[400])),
                                keyboardType: TextInputType.emailAddress,
                                validator: (String value) { <= this line throw error
                                  if (value.isEmpty) {
                                    return 'Please Enter Name';
                                  }
                                  return null;
                                },
                                onSaved: (String value) {   <= this line throw error
                                 name = value;
                                },
                              )

“验证器:(字符串值)”抛出:

无法将参数类型“void Function(String)”分配给参数 键入'void Function(String?)?'.dartargument_type_not_assignable

“onSaved:(字符串值)”抛出:

参数类型“void Function(String)”不能分配给 参数类型 'void 函数(字符串?)?'.dartargument_type_not_assignable

当我检查 GitHub 代码时,我看到了相同的结构,但它没有抛出错误

child: TextFormField(
                 keyboardType: TextInputType.text,
                 decoration: buildInputDecoration(Icons.person, "Full Name"),
                 validator: (String value) {
                   if (value.isEmpty) {
                     return 'Please Enter Name';
                   }
                   return null;
                 },
                 onSaved: (String value) {
                   name = value;
                 },
               ),

“buildInputDecoration”函数:

InputDecoration buildInputDecoration(IconData icons,String hinttext) {
  return InputDecoration(
    hintText: hinttext,
    prefixIcon: Icon(icons),
    focusedBorder: OutlineInputBorder(
      borderRadius: BorderRadius.circular(25.0),
      borderSide: BorderSide(
          color: Colors.green,
          width: 1.5
      ),
    ),
    border: OutlineInputBorder(
      borderRadius: BorderRadius.circular(25.0),
      borderSide: BorderSide(
        color: Colors.blue,
        width: 1.5,
      ),
    ),
    enabledBorder:OutlineInputBorder(
      borderRadius: BorderRadius.circular(25.0),
      borderSide: BorderSide(
        color: Colors.blue,
        width: 1.5,
      ),
    ),
  );
}

【问题讨论】:

    标签: flutter validation dart textformfield


    【解决方案1】:

    您在 GitHub 中的示例适用于没有 null 安全性的较旧版本的 Dart。您正在使用具有 null 安全性的 Dart,其中 StringString? 的类型不同。所以只需在String 之后添加? 到验证器函数参数:

        return TextFormField(
          ...
          validator: (String? value) {
            if (value?.isEmpty ?? true) {
              return 'Please Enter Name';
            }
            return null;
          },
          onSaved: (String? value) {
            name = value;
          },
        );
    

    您也可以只删除显式 value 类型:

        return TextFormField(
          ...
          validator: (value) {
            if (value?.isEmpty ?? true) {
              return 'Please Enter Name';
            }
            return null;
          },
          onSaved: (value) {
            name = value;
          },
        );
    

    【讨论】:

      猜你喜欢
      • 2016-05-01
      • 1970-01-01
      • 2019-05-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多