Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Null check operator used on a null value after pressing Done on the keyboard in flutter

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

>Solution :

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));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading