我通過將列小部件包裝Form()成這樣來創建一個表單
Form(
key: formKey,
child: Column(
children: [
但是當我嘗試設定final isValid = formKey.currentState!.validate();檢查時,我得到一個例外已經發生訊息_CastError (Null check operator used on a null value)
任何建議?示例圖片
uj5u.com熱心網友回復:
此時 currentState 為空,您正在嘗試在構建表單之前對其進行驗證。當單擊某些提交按鈕等時,在回呼中驗證表單會更有意義。檢查鏈接到顫振檔案以獲取示例。
uj5u.com熱心網友回復:
您需要創建這樣的函式并在單擊某些提交按鈕時呼叫,等等。謝謝。
void _saveForm() {
final isValid = formKey.currentState!.validate();
if (!isValid) {
// ignore: avoid_returning_null_for_void
return null;
}
_form.currentState!.save();
// .... extra code
}
uj5u.com熱心網友回復:
部分感謝 Void Void 解決方案不是宣告一個變數 isVaild。檢查formKey.currentState!.validate()按下按鈕
onPressed: () {
if (!formKey.currentState!.validate()) return;
AccountUpdate.updatePassword(
_confirmController.text, context);
},
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/467984.html
