Home > OS >  How can i check and retrieve empty double value using Flutter
How can i check and retrieve empty double value using Flutter

Time:09-29

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

CodePudding user response:

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");
  • Related