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;
},
),
),
您是否尝试过构建一个自定义验证器函数,然后从验证器属性直接调用它。
例如:-
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';
}
}
这是一个不完整的问题,这个扩展框数组扰乱了验证器:
ExpansionPanelList.radio(initialOpenPanelValue: 2,
children: [
bicyclePanel,
carPanel,
floodPanel,
diyPanel,
surplusPanel,
gardeningPanel,
ballSportsPanel,
snowSportsPanel,
waterSportsPanel,
campingPanel,
backpackingPanel,
circusPanel,
]),
我推测,当调用_formKeyForDeposit.currentState.validate()
时,它会进入ExpansionPanelList,并且无法逃脱以触发其上方TextFormFields的验证器。
由于我在ExpansionPanelList的宽度之外只有1个TextFormField,所以我使用_controllerDefaultDeposit.text
来获取Deposit FormField值并手动验证它。这是一个棘手的解决方案,但目前可以。