[英]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;
},
)
“驗證器:(字符串值)”拋出:
參數類型 'void Function(String)' 不能分配給參數類型 'void Function(String?)?'.dartargument_type_not_assignable
"onSaved: (String value)" 拋出:
參數類型 'void Function(String)' 不能分配給參數類型 'void Function(String?)?'.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,
),
),
);
}
您來自 GitHub 的示例適用於沒有空安全的舊 Dart 版本。 您正在使用具有空安全性的 Dart,其中String
與String
的類型不同String?
. 所以只需添加?
在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;
},
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.