Widget _buildName(){
return TextFormField(
decoration: InputDecoration(labelText: "Name"),
validator: (String value){
if(value.isEmpty){
return 'Name is Required';
}
},
onSaved: (String value){
_name = value;
}
);
}
如果检查onSaved
和validator
方法,它们都提供可为null的字符串
FormFieldSetter<String>? onSaved,
FormFieldValidator<String>? validator,
所以它将是
TextFormField(
decoration: InputDecoration(labelText: "Name"),
validator: (String? value) {
// add other condition
if (value == null) {
return 'Name is Required';
}
return null;
},
onSaved: (String? value) {
_name = value;
});
有关TextFormField 的更多信息