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

flutter调试

上面的调试The following FormatException was thrown while handling a gesture:
Invalid double
-Infinity874When 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#1c367debugOwner: GestureDetectorstate: possiblewon arenafinalPosition: Offset(1109.0, 314.0)finalLocalPosition: Offset(160.0, 43.0)button: 1sent 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.mrgr.cn/news/70758.html

相关文章:

  • 2.操作系统常见面试问题3
  • WPF 应用程序中使用 Prism 框架时,有多种方式可以注册服务和依赖项
  • Request和Response
  • adb 命令查看设备存储占用情况
  • 编程之路,从0开始:练习篇
  • HTMLCSS: 日落卡片
  • Ansys EMC Plus:以 Touchstone 格式计算和导出 S 参数
  • Python爬虫
  • 双指针(二)双指针到底是怎么个事
  • POP3、SMTP、FTP、HTTP、BGP、DNS、DHCP、RIP、Ping、Traceroute
  • 十、有C/C++/Java基础,迅速掌握Python,B站黑马2022版教程笔记(自用)
  • 高字节,低字节,大端序,小端序
  • 学习python的第九天之数据类型——tuple元组
  • 数学建模模型算法-Python实现
  • 自动渗透测试与手动渗透测试
  • springboot和vue:十六、JWT跨域认证代码(Cookie、Session、Token)
  • 深入浅出JUC常用同步器
  • 【漏洞复现】用友 U8CRM leadconversion.php Sql注入漏洞
  • 基于STM32U575RIT6智能除湿器项目
  • 【星闪EBM-H63开发板】AT固件的配置与测试
  • 121页PPT | 企业战略规划全景:战略设计、工具、模板和分析方法
  • JAVA完成猜数字小游戏
  • python练习-Django web入门
  • STM32:ADC
  • 万字长文解读机器学习——决策树
  • [C++]——位图与布隆过滤器