The method 'validate' was called on null.
Receiver: null
Tried calling: validate()
我不明白这一点。我想也许问题是 Form 不是类的根元素,它不是return Form(child: Column(children: [...
所以我尝试将 Form Widget 设为根,它停止了错误,但没有激活 TextFormField 验证器或保存,它只是说'一切都好,继续前进'。
这只是我目前希望验证的一个领域。我查找了其他此类查询,Form 小部件和 TextFormField 都有键,所以我被卡住了。
我声明表单键final _formKeyForDeposit = GlobalKey<FormState>();
这是不合作的形式:
Form(key: _formKeyForDeposit, child:
TextFormField(
controller: _controllerDefaultDeposit,
key: Key('defaultLoanDeposit'),
decoration: InputDecoration(
//icon: Icon(Icons.location_city),
labelText: 'Per item deposit',
hintText: 'Whole numbers',
suffixIcon: IconButton(
icon: Icon(Icons.clear),
onPressed: () {
_controllerDefaultDeposit.clear();
},
),
),
keyboardType: TextInputType.numberWithOptions(decimal: false, signed: false),
onSaved: (String amountStr) {
print("saving deposit");
user.defaultItemDeposit = int.parse(amountStr.trim());
},
validator: (String value) {
print(LOG + "validator called");
if(int.tryParse(value.trim()) == null) {
inputCompletionAlert += "But your default item deposit is not a number, please correct.\n";
return 'Not a £-- whole number monetary amount';
}
if(value == "" || value == "0") {
print(LOG + 'deposit validator called, should launch Dialog from here');
inputCompletionAlert += "Would you like to set a default deposit?";
return "Would you like to set a deposit?";
}
return null;
},
),
),