当前位置: 首页 > article >正文

flutter调试

上面的调试The following FormatException was thrown while handling a gesture:
Invalid double
-Infinity874

When the exception was thrown, this was the stack: 
#0      double.parse (dart:core-patch/double_patch.dart:113:28)
#1      _CalculatorScreenState._buttonPressed.<anonymous closure> (package:cluac2/main.dart:105:24)
#2      State.setState (package:flutter/src/widgets/framework.dart:1203:30)
#3      _CalculatorScreenState._buttonPressed (package:cluac2/main.dart:96:5)
#4      _CalculatorScreenState._buildButton.<anonymous closure> (package:cluac2/main.dart:273:28)
#5      _InkResponseState.handleTap (package:flutter/src/material/ink_well.dart:1170:21)
#6      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:351:24)
#7      TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:656:11)
#8      BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:313:5)
#9      BaseTapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:246:7)
#10     PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:703:9)
#11     PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:98:12)
#12     PointerRouter._dispatchEventToRoutes.<anonymous closure> (package:flutter/src/gestures/pointer_router.dart:143:9)
#13     _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:633:13)
#14     PointerRouter._dispatchEventToRoutes (package:flutter/src/gestures/pointer_router.dart:141:18)
#15     PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:127:7)
#16     GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:501:19)
#17     GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:481:22)
#18     RendererBinding.dispatchEvent (package:flutter/src/rendering/binding.dart:450:11)
#19     GestureBinding._handlePointerEventImmediately (package:flutter/src/gestures/binding.dart:426:7)
#20     GestureBinding.handlePointerEvent (package:flutter/src/gestures/binding.dart:389:5)
#21     GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:336:7)
#22     GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:305:9)
#23     _invoke1 (dart:ui/hooks.dart:328:13)
#24     PlatformDispatcher._dispatchPointerDataPacket (dart:ui/platform_dispatcher.dart:442:7)
#25     _dispatchPointerDataPacket (dart:ui/hooks.dart:262:31)
Handler: "onTap"
Recognizer: TapGestureRecognizer#1c367
  debugOwner: GestureDetector
  state: possible
  won arena
  finalPosition: Offset(1109.0, 314.0)
  finalLocalPosition: Offset(160.0, 43.0)
  button: 1
  sent tap down

ai建议

从你提供的错误信息来看,FormatException: Invalid double -Infinity874 通常表示在尝试将字符串转换为双精度浮点数时出现了问题。在你的计算器应用中,这种情况通常发生在输入不符合预期的情况下,例如尝试解析一个未定义的值或 Infinity。

解决方案
检查输入值:确保在调用 double.parse 之前,输入的字符串是有效的数字。可以使用条件语句来避免解析无效字符串。

处理特殊情况:添加逻辑以处理特定的操作符和边界条件,例如除以零或计算负数的平方根等。

以下是改进后的 _buttonPressed 方法,它增加了一些安全检查,以确保不会对无效字符串执行解析

void _buttonPressed(String buttonText) {
  setState(() {
    _playSound(buttonText); // 按钮被按下时播放音效
    if (buttonText == "C") {
      _output = "0";
      _num1 = 0;
      _num2 = 0;
      _operator = "";
      _displayText = "";
    } else if (["+", "-", "x", "÷"].contains(buttonText)) {
      _num1 = _tryParseDouble(_output);
      _operator = buttonText;
      _displayText = "$_output $buttonText";
      _output = "0";
    } else if (buttonText == "=") {
      _num2 = _tryParseDouble(_output);
      double result = 0;
      if (_operator == "+") {
        result = _num1 + _num2;
      } else if (_operator == "-") {
        result = _num1 - _num2;
      } else if (_operator == "x") {
        result = _num1 * _num2;
      } else if (_operator == "÷") {
        if (_num2 != 0) {
          result = _num1 / _num2;
        } else {
          _output = "Error"; // 避免除以零
          return;
        }
      }
      _output = result.toString();
      String historyItem = "$_num1 $_operator $_num2 = $_output";
      _history.add(historyItem);
      _saveHistory(); // 保存历史记录
    } else if (buttonText == "√") {
      double num = _tryParseDouble(_output);
      if (num >= 0) {
        double sqrtResult = math.sqrt(num);
        _output = sqrtResult.toString();
      } else {
        _output = "Error"; // 处理负数的平方根
      }
    } else if (buttonText == "%") {
      double num = _tryParseDouble(_output);
      double percentage = num / 100;
      _output = percentage.toString();
    } else if (buttonText == ".") {
      if (!_output.contains(".")) {
        _output += ".";
      }
    } else if (buttonText == "±") {
      double num = _tryParseDouble(_output);
      num *= -1;
      _output = num.toString();
    } else if (buttonText == "←") {
      if (_output.length > 1) {
        _output = _output.substring(0, _output.length - 1);
      } else {
        _output = "0";
      }
    } else if (buttonText == "x²") {
      double num = _tryParseDouble(_output);
      double squareResult = num * num;
      _output = squareResult.toString();
    } else if (buttonText == "㏒") {
      double num = _tryParseDouble(_output);
      if (num > 0) {
        double logResult = math.log(num);
        _output = logResult.toString();
      } else {
        _output = "Error"; // 对于非正数的对数
      }
    } else if (buttonText == "sin") {
      double num = _tryParseDouble(_output);
      double sinResult = math.sin(num * math.pi / 180);
      _output = sinResult.toString();
    } else {
      _output = _output == "0" ? buttonText : _output + buttonText;
    }
  });
}

double _tryParseDouble(String value) {
  try {
    return double.parse(value);
  } catch (e) {
    // 如果解析失败,返回 0 或其他安全值
    return 0;
  }
}

修改要点
引入 _tryParseDouble 方法:这个方法尝试将字符串解析为双精度浮点数,如果解析失败则返回 0。这可以防止因无效输入导致的异常。
检查除法和平方根:在进行除法和求平方根之前,确认分母不为零且被平方的数为非负数。如果条件不满足,则输出一个错误提示。
通过这些调整,可以更好地处理输入并避免 FormatException 错误。请根据需求进行测试,并确保其他功能正常工作。如有任何问题,请继续询问!

还是无声音
flutter: Error playing sound: Unable to load asset: “assets/assets/123_04.mp3”.


http://www.kler.cn/a/391386.html

相关文章:

  • HTTP、HTTPS和SOCKS5代理協議
  • mysql-主从同步与读写分离
  • PromptGIP:Unifying lmage Processing as Visual Prompting Question Answering
  • SAP抓取外部https报错SSL handshake处理方法
  • unity webgl部署到iis报错
  • 深度学习实战车辆目标跟踪【bytetrack/deepsort】
  • D3入门:学习思维导图 + 99个中文API详解
  • 【C++】 C++游戏设计---五子棋小游戏
  • Qt生成coredump文件(支持arm和x86架构)
  • opencv保姆级讲解——光学学符识别(OCR)(4)
  • Docker部署Nginx服务器并实现HTTPS自动重定向
  • 【蓝桥等考C++真题】蓝桥杯等级考试C++组第13级L13真题原题(含答案)-成绩排序
  • 【ECMAScript标准规范】
  • 「QT」基础数据类 之 QVariant 通用数据类
  • PHY6235超低功耗蓝牙和专有2.4G应用的SOC芯片内置MCU
  • Git 中的 patch 功能
  • 生成式模型的热点新闻和进展
  • 第8章利用CSS制作导航菜单
  • 鸿蒙ZRouter动态路由框架—生命周期管理能力
  • 论云游戏的性能与性价比,ToDesk、青椒云、顺网云游戏等具体实操看这篇就够了
  • SAP ABAP开发学习——function alv复选框设置
  • [论文阅读]Enhanced Membership Inference Attacks against Machine Learning Models
  • jmeter基础05_第1个http请求
  • Java基于SpringBoot+Vue的宠物共享平台的设计与实现(附源码,文档)
  • 22. 记录架构
  • 【热门主题】000041 网络安全:守护数字世界的坚固防线