【问题标题】:How can i check and retrieve empty double value using Flutter如何使用 Flutter 检查和检索空的双精度值
【发布时间】:2021-11-21 00:48:02
【问题描述】:

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

错误:- ════════ 手势捕获的异常 ══════════════════════════════════════════以下 处理手势时抛出 FormatException: Invalid double

抛出异常时,这是堆栈 #0 double.parse (dart:core-patch/double_patch.dart:111:28) #1 _HomePageState.build.. 包:urban_laundry/page/home_page.dart:992 #2 State.setState 包:flutter/…/widgets/framework.dart:1088 #3 _HomePageState.build。包:urban_laundry/page/home_page.dart:975 #4 _InkResponseState._handleTap 包:flutter/…/material/ink_well.dart:989 #5 GestureRecognizer.invokeCallback 包:flutter/…/gestures/recognizer.dart:193 #6 TapGestureRecognizer.handleTapUp 包:flutter/…/gestures/tap.dart:608 #7 BaseTapGestureRecognizer._checkUp 包:flutter/…/gestures/tap.dart:296 #8 BaseTapGestureRecognizer.handlePrimaryPointer 包:flutter/…/gestures/tap.dart:230 #9 PrimaryPointerGestureRecognizer.handleEvent 包:flutter/…/gestures/recognizer.dart:558 #10 PointerRouter._dispatch 包:flutter/…/gestures/pointer_router.dart:94 #11 PointerRouter._dispatchEventToRoutes。包:flutter/…/gestures/pointer_router.dart:139 #12 _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:400:8) #13 PointerRouter._dispatchEventToRoutes 包:flutter/…/gestures/pointer_router.dart:137 #14 PointerRouter.route 包:flutter/…/gestures/pointer_router.dart:123 #15 GestureBinding.handleEvent 包:flutter/…/gestures/binding.dart:440 #16 GestureBinding.dispatchEvent 包:flutter/…/gestures/binding.dart:420 #17 RendererBinding.dispatchEvent 包: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();
    });
  },

【问题讨论】:

    标签: flutter flutter-desktop


    【解决方案1】:

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

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

    【讨论】:

    • lib/page/home_page.dart:990:71:警告:null-aware 操作的操作数 '?.'具有不包括 null 的“TextEditingController”类型。 - 'TextEditingController' 来自'package:flutter/src/widgets/editable_text.dart' ('/C:/src/flutter/packages/flutter/lib/src/widgets/editable_text.dart')。交付 = double.parse(deliveryCharge?.text??"0");
    • 您可以将?. 替换为 dot(.) 并更新我的答案
    • 感谢您的回复
    • lib/page/home_page.dart:994:58:警告:null-aware 操作的操作数 '??'具有不包括空值的“字符串”类型。 price3.text ?? "0"); ^
    • 否则你可以试试double.parse(price3.text!=null && price3.text.isNotEmpty?price3.text:"0")
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-10
    • 1970-01-01
    • 1970-01-01
    • 2015-04-11
    • 1970-01-01
    相关资源
    最近更新 更多