[英]the validation of my TextFormField always throw an error
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;
},
)
"validator: (String value)" throws: “验证器:(字符串值)”抛出:
The argument type 'void Function(String)' can't be assigned to the parameter type 'void Function(String?)?'.dartargument_type_not_assignable
参数类型 'void Function(String)' 不能分配给参数类型 'void Function(String?)?'.dartargument_type_not_assignable
"onSaved: (String value)" throws: "onSaved: (String value)" 抛出:
The argument type 'void Function(String)' can't be assigned to the parameter type 'void Function(String?)?'.dartargument_type_not_assignable
参数类型 'void Function(String)' 不能分配给参数类型 'void Function(String?)?'.dartargument_type_not_assignable
when I checked a GitHub code I saw the same structure but it's not throwing an error当我检查 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" function : “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,
),
),
);
}
Your example from GitHub is for older Dart versions without null safety.您来自 GitHub 的示例适用于没有空安全的旧 Dart 版本。 You are using Dart with null safety where
String
is not the same type as String?
您正在使用具有空安全性的 Dart,其中
String
与String
的类型不同String?
. . So just add
?
所以只需添加
?
after String
to validator function parameter:在
String
到验证器函数参数之后:
return TextFormField(
...
validator: (String? value) {
if (value?.isEmpty ?? true) {
return 'Please Enter Name';
}
return null;
},
onSaved: (String? value) {
name = value;
},
);
You can also just remove explicit value
type:您也可以删除显式
value
类型:
return TextFormField(
...
validator: (value) {
if (value?.isEmpty ?? true) {
return 'Please Enter Name';
}
return null;
},
onSaved: (value) {
name = value;
},
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.