Please tell me, I have a problem with textfield. When I finished entering data in the field, I press the Done key on the keyboard and I get this error Null check operator used on a null value
although all data is saved. And which operator has a null value, I can’t understand, after all, everything is saved. How can I remove this error or warning?
text_field
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));
},
error
CodePudding user response:
An error occurs at this point:
widget.onChanged!(double.parse(value));
You have to be sure that widget.onChanged
isn't null.
The best way is:
if (widget.onChanged != null) {
widget.onChanged!(double.parse(value));
}
or
widget.onChanged?.call(double.parse(value));