我试图在模型中分配输入值,以便在API POST请求中使用它。但是,我不断收到错误消息:
setter was called on null when saving
型号:
@JsonSerializable()
class Discount {
String value;
Discount();
set setValue(String value) {
this.value = value;
}
Dart代码:
children: <Widget>[
Expanded(
child: Container(
margin: EdgeInsets.only(right: 5),
child: TextFormField(
initialValue: _invoiceFormData.discount?.value ?? '0.00',
inputFormatters: [_amountValidator],
keyboardType: TextInputType.numberWithOptions(
decimal: true,
signed: false,
),
decoration: TextFormField.decoration(
labelText: Localizations.of(context)
.text('label_invoice_discount'),
),
onChanged: (String value) {
setState(() {
discountTotal = value;
});
},
onSaved: (value) => _invoiceFormData
.discount.setValue = value,
),
),
),
FlatButton(
onPressed: () {
setState(() {
discountType = !discountType;
});
},
),
],
日志:
The setter 'setValue=' was called on null.
Receiver: null
Tried calling: setValue="10.00"
我尝试设置_invoiceFormData.discount.value = value
,但仍显示相同的错误。
答案 0 :(得分:0)
使用StatefulWidget类和
更改此
onSaved: (value) => _invoiceFormData
.discount.setValue = value,
到
onSaved: (value) => setState(() {
onSaved: (value) => _invoiceFormData
.discount.setValue = value,
});
答案 1 :(得分:0)
当您使用设置器_invoiceFormData.discount
时,您的日志说setValue=
为空。
要解决此问题,您首先需要在名为discount
的对象中实例化字段_invoiceFormData
。例如,当您初始化此表单数据(也许在initState()中)时,应执行_invoiceFormData.discount = Discount();
,然后它将不再为null