上面的调试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".