我如何使用 Flutter 检查和检索空的双精度值

How can i check and retrieve empty double value using Flutter

我正在处理 flutter 桌面项目,我想检索一些双精度值,但是当将某些字段作为空字段进行处理并尝试检索数据时,我得到了一个错误来填充我拥有的所有 TextField。我如何将这些 TextField 检查为空字段并检索空值。

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

代码:-

  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);
    });
  }

文本字段:-

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',
      ),
    ),
  ),
),

按下:-

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

如果您的数据为空,您可以将其替换为零。试试吧。

      delivery = double.parse(deliveryCharge.text??"0");
      p1 = double.parse(price1.text??"0");
      p2 = double.parse(price2.text??"0");
      p3 = double.parse(price3.text??"0");