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".

相关推荐
迷雾漫步者7 小时前
Flutter组件————FloatingActionButton
前端·flutter·dart
coder_pig12 小时前
📝小记:Ubuntu 部署 Jenkins 打包 Flutter APK
flutter·ubuntu·jenkins
捡芝麻丢西瓜14 小时前
flutter自学笔记5- dart 编码规范
flutter·dart
恋猫de小郭14 小时前
什么?Flutter 可能会被 SwiftUI/ArkUI 化?全新的 Flutter Roadmap
flutter·ios·swiftui
sunly_2 天前
Flutter:导航,tab切换,顶部固定,列表分页滚动
开发语言·javascript·flutter
敲代码的小强2 天前
Flutter项目兼容鸿蒙Next系统
flutter·华为·harmonyos
Zh-jie2 天前
flutter 快速实现侧边栏
前端·javascript·flutter
truemi.733 天前
flutter --no-color pub get 超时解决方法
android·flutter
王家视频教程图书馆3 天前
flutter 使用dio 请求go语言后台数据接口展示瀑布流图片
flutter
迷雾漫步者3 天前
Flutter组件————AppBar
flutter·跨平台·dart