0

I am working with flutter desktop project, i want to retrieve some double values but when proceed some field as an empty field and try to retrieve the data and i got an error to fill up all the TextField i have. How can i check those TextField as an empty field and retrieve an empty value.

Error:- ════════ Exception caught by gesture ═══════════════════════════════════════════ The following FormatException was thrown while handling a gesture: Invalid double

When the exception was thrown, this was the stack #0 double.parse (dart:core-patch/double_patch.dart:111:28) #1 _HomePageState.build.. package:urban_laundry/page/home_page.dart:992 #2 State.setState package:flutter/…/widgets/framework.dart:1088 #3 _HomePageState.build. package:urban_laundry/page/home_page.dart:975 #4 _InkResponseState._handleTap package:flutter/…/material/ink_well.dart:989 #5 GestureRecognizer.invokeCallback package:flutter/…/gestures/recognizer.dart:193 #6 TapGestureRecognizer.handleTapUp package:flutter/…/gestures/tap.dart:608 #7 BaseTapGestureRecognizer._checkUp package:flutter/…/gestures/tap.dart:296 #8 BaseTapGestureRecognizer.handlePrimaryPointer package:flutter/…/gestures/tap.dart:230 #9 PrimaryPointerGestureRecognizer.handleEvent package:flutter/…/gestures/recognizer.dart:558 #10 PointerRouter._dispatch package:flutter/…/gestures/pointer_router.dart:94 #11 PointerRouter._dispatchEventToRoutes. package:flutter/…/gestures/pointer_router.dart:139 #12 _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:400:8) #13 PointerRouter._dispatchEventToRoutes package:flutter/…/gestures/pointer_router.dart:137 #14 PointerRouter.route package:flutter/…/gestures/pointer_router.dart:123 #15 GestureBinding.handleEvent package:flutter/…/gestures/binding.dart:440 #16 GestureBinding.dispatchEvent package:flutter/…/gestures/binding.dart:420 #17 RendererBinding.dispatchEvent package:flutter/…/rendering/binding.dart:278

Code :-

  final price1 = TextEditingController();
  final price2 = TextEditingController();
  final price3 = TextEditingController();

  final deliveryCharge = TextEditingController();

  double? p1;
  double? p2;
  double? p3;
  double? delivery;
  double total = 0.0;

  void priceTotal() {
    double p1 = double.parse(price1.text);
    double p2 = double.parse(price2.text);
    double p3 = double.parse(price3.text);

    double delivery = double.parse(deliveryCharge.text);

    print(total.toString());
    setState(() {
      total = (p1 + p2 + p3 + delivery);
    });
  }

TextField :-

Padding(
  padding: const EdgeInsets.only(
      top: 10.0, left: 10.0),
  child: SizedBox(
    width: 200.0,
    child: TextField(
      controller: price3,
      decoration: const InputDecoration(
        border: OutlineInputBorder(
          borderRadius: BorderRadius.all(
              Radius.circular(10.0)),
        ),
        prefixIcon: Padding(
          padding: EdgeInsets.all(8.0),
          child: FaIcon(
            FontAwesomeIcons.rupeeSign,
            size: 20.0,
          ),
        ),
        hintText: 'Price',
      ),
    ),
  ),
),

OnPress :-

  onPressed: () {
    setState(() {
      delivery = double.parse(deliveryCharge.text);
      p1 = double.parse(price1.text);
      p2 = double.parse(price2.text);
      p3 = double.parse(price3.text);
      priceTotal();
    });
  },

Image Here

Dren
  • 21
  • 9

1 Answers1

0

If your data is null, you can replace it with zero. Let's try.

      delivery = double.parse(deliveryCharge.text??"0");
      p1 = double.parse(price1.text??"0");
      p2 = double.parse(price2.text??"0");
      p3 = double.parse(price3.text??"0");
Jahidul Islam
  • 11,435
  • 3
  • 17
  • 38
  • lib/page/home_page.dart:990:71: Warning: Operand of null-aware operation '?.' has type 'TextEditingController' which excludes null. - 'TextEditingController' is from 'package:flutter/src/widgets/editable_text.dart' ('/C:/src/flutter/packages/flutter/lib/src/widgets/editable_text.dart'). delivery = double.parse(deliveryCharge?.text??"0"); – Dren Sep 29 '21 at 05:51
  • you can replace `?.` with just dot(.) and also updated my answer – Jahidul Islam Sep 29 '21 at 05:57
  • Thankyou for your response – Dren Sep 29 '21 at 06:08
  • lib/page/home_page.dart:994:58: Warning: Operand of null-aware operation '??' has type 'String' which excludes null. price3.text ?? "0"); ^ – Dren Sep 29 '21 at 06:08
  • 1
    otherwise you can try `double.parse(price3.text!=null && price3.text.isNotEmpty?price3.text:"0")` – Jahidul Islam Sep 29 '21 at 06:10
  • Yes this work, but can we enter empty string instead of "0" – Dren Sep 29 '21 at 06:14
  • Thank you so much. How can i make as empty " " – Dren Sep 29 '21 at 06:17
  • don't replace zero, double parse can't convert the empty string – Jahidul Islam Sep 29 '21 at 06:30