請告訴我,我的文本欄位有問題。當我在欄位中輸入完資料后,按鍵盤上的 Done 鍵,Null check operator used on a null value
盡管所有資料都已保存,但仍出現此錯誤。而哪個算子有空值,我也看不懂,畢竟一切都保存了。如何洗掉此錯誤或警告?
文本域
class PriceCounter extends StatefulWidget {
PriceCounter({Key? key, required this.price, this.onChanged})
: super(key: key);
double price;
final Function(double)? onChanged;
@override
State<PriceCounter> createState() => _PriceCounterState();
}
class _PriceCounterState extends State<PriceCounter> {
final _priceController = TextEditingController();
@override
void dispose() {
_priceController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
final FilterPriceCubit cubit = BlocProvider.of<FilterPriceCubit>(context);
return BlocBuilder<FilterPriceCubit, FilterPriceState>(
builder: (context, state) {
_priceController.text = state.fitlerPrice.toStringAsFixed(2).toString();
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 21),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
IconButton(
onPressed: () => cubit.priceFilterDecrement(state.fitlerPrice),
icon: SvgPicture.asset(constants.Assets.minus),
constraints: const BoxConstraints(),
padding: EdgeInsets.zero,
),
const SizedBox(width: 20),
SizedBox(
width: 100,
child: TextFormField(
keyboardType: TextInputType.number,
// controller: _priceController
// ..text = widget.price.toStringAsFixed(2),
controller: _priceController,
style: constants.Styles.normalBookTextStyleWhite,
textAlign: TextAlign.center,
decoration: const InputDecoration(
prefix: Text('JC',
style: constants.Styles.normalBookTextStyleWhite),
suffix: Text(
'KWh',
style: constants.Styles.smallerBookTextStyleWhite,
),
contentPadding: EdgeInsets.zero,
border: InputBorder.none,
),
onFieldSubmitted: (value) {
cubit.setPrice(value);
widget.onChanged!(double.parse(value));
},
錯誤

uj5u.com熱心網友回復:
此時出現錯誤:
widget.onChanged!(double.parse(value));
您必須確保它widget.onChanged不為空。
最好的方法是:
if (widget.onChanged != null) {
widget.onChanged!(double.parse(value));
}
或者
widget.onChanged?.call(double.parse(value));
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/487754.html
