【问题标题】:flutter simple calculation Function errorflutter 简单计算 函数错误
【发布时间】:2021-05-11 09:17:14
【问题描述】:

当我一次调用该函数时,它运行良好,但如果我再次调用它,它将显示不同的值并读取不同的 else 块,请检查我附加整个类和驱动程序代码的代码以及以下函数的输出 我认为 if else 条件块中有错误我不明白为什么它在第二次调用函数时给出不同的值如果我再次运行它第三次它将给出正确的输出

enum ComponentCondition { missing, damaged, good }
class ComponentModel {
  String name;
  ComponentCondition condition;

  double rate;

  double componentPercent;
  double kerbWeight;
  double componentPrice;

  int lifespan;
  int age;
  ComponentModel(
      {this.name,
      @required this.rate,
      @required this.lifespan,
      @required this.kerbWeight,
      @required this.componentPercent,
      @required this.age});

  int calculatePrice() {
    double weight = (componentPercent * kerbWeight) / 100;
    age = DateTime.now().year - age;
    double scrapValue = weight * rate;
    int i = lifespan ~/ 3;

    if ((condition == ComponentCondition.good ||
            condition == ComponentCondition.damaged) &&
        i == 0) {
      componentPrice = scrapValue;
    } else if (condition == ComponentCondition.good && age < lifespan) {
      if (age >= 0 && age <= i) {
        print('bestconditionPrice');
        componentPrice = scrapValue * 3;
      } else if (age > i && age <= i * 2) {
        componentPrice = scrapValue * 2;
      } else if (age > i * 2 && age < lifespan) {
        componentPrice = scrapValue * 1.5;
      }
    } else if ((age >= lifespan) || condition == ComponentCondition.damaged) {
      componentPrice = scrapValue;
      print('why??????');
    } else
      componentPrice = 0;

    return componentPrice.toInt();
  }

  void updateCondition(ComponentCondition partCondition) {
    condition = partCondition;
  }
}

void main(List<String> args) {
  ComponentModel engine = ComponentModel(
      componentPercent: 18, kerbWeight: 695, rate: 17, lifespan: 13, age: 2021);
  engine.updateCondition(ComponentCondition.good);
  int price = engine.calculatePrice();
  int price2 = engine.calculatePrice();
  print(' price1 :$price');
  print("price2 : $price2");
 
}

【问题讨论】:

    标签: flutter runtime-error calculation


    【解决方案1】:

    首先,这是一个问得很好的问题,你提出了一个完全可重现的例子,所以恭喜。

    至于你的问题,我不完全确定你想要发生什么,但我会告诉你要改变什么,所以你的编译器会告诉你在哪里你错了:

    • 使用最新版本的 Flutter。它支持空安全。
    • 更多地使用final 关键字。您不希望更改的每个变量都将它们设为final。这样,当您意外更改它们时,您的编译器会抛出错误,您会立即注意到。
    • 初始化所有数据。 Null-safety 将在这里为您提供帮助,因为当您忘记时,您将再次遇到编译错误。
    • 不要仅仅因为可以保留实例数据。如果某物可以是局部变量,使它成为局部变量。
    • 请保持方法简洁明了,因此当您可以简单地返回并完成它时,不要陷入无休止的 if/then 链。

    因此,经过一些最佳实践更改后,您的代码如下所示:

    enum ComponentCondition { missing, damaged, good }
    
    class ComponentModel {
      String? name;
      ComponentCondition condition = ComponentCondition.good;
    
      final double rate;
      final double componentPercent;
      final double kerbWeight;
      final int lifespan;
      final int age;
      
      ComponentModel(
          {this.name,
          required this.rate,
          required this.lifespan,
          required this.kerbWeight,
          required this.componentPercent,
          required this.age });
    
      int calculatePrice() {
        final weight = (componentPercent * kerbWeight) / 100;
        final double scrapValue = weight * rate;
        final i = lifespan ~/ 3;
        
        age = DateTime.now().year - age;
    
        if ((condition == ComponentCondition.good || condition == ComponentCondition.damaged) && i == 0) {
          return scrapValue.toInt();
        }
        
        if (condition == ComponentCondition.good && age < lifespan) {
          if (age >= 0 && age <= i) {
            print('bestconditionPrice');
            return (scrapValue * 3).toInt();
          } 
          
          if (age > i && age <= i * 2) {
            return (scrapValue * 2).toInt();
          } 
          
          if (age > i * 2 && age < lifespan) {
            return (scrapValue * 1.5).toInt();
          }
        } 
        
        if ((age >= lifespan) || condition == ComponentCondition.damaged) {
          return scrapValue.toInt();
        }
        
        return 0;
      }
    
      void updateCondition(ComponentCondition partCondition) {
        condition = partCondition;
      }
    }
    
    void main(List<String> args) {
      ComponentModel engine = ComponentModel(componentPercent: 18, kerbWeight: 695, rate: 17, lifespan: 13, age: 2021);
      engine.updateCondition(ComponentCondition.good);
      final price = engine.calculatePrice();
      final price2 = engine.calculatePrice();
      print('price1 :$price');
      print('price2 : $price2');
    } 
    

    现在你会得到一个明显的编译器错误:

    错误:没有为类“ComponentModel”定义设置器“age”。

    • “ComponentModel”来自“package:dartpad_sample/main.dart”(“lib/main.dart”)。 年龄 = DateTime.now().year - 年龄; ^^^ 错误:编译失败。

    太棒了。编译器刚刚发现了你的错误。您不小心重新分配了age 变量,而不是使用局部变量,可以说是“年”。也许您应该将this.age 重命名为this.ModelMakeYear,然后您的age 局部变量将是完美的。

    因此,通过这些更改,您的代码应该如下所示:

    enum ComponentCondition { missing, damaged, good }
    
    class ComponentModel {
      String? name;
      ComponentCondition condition = ComponentCondition.good;
    
      final double rate;
      final double componentPercent;
      final double kerbWeight;
      final int lifespan;
      final int modelMakeYear;
      
      ComponentModel(
          {this.name,
          required this.rate,
          required this.lifespan,
          required this.kerbWeight,
          required this.componentPercent,
          required this.modelMakeYear });
    
      int calculatePrice() {
        final weight = (componentPercent * kerbWeight) / 100;
        final double scrapValue = weight * rate;
        final i = lifespan ~/ 3;
        
        final age = DateTime.now().year - modelMakeYear;
    
        if ((condition == ComponentCondition.good || condition == ComponentCondition.damaged) && i == 0) {
          return scrapValue.toInt();
        }
        
        if (condition == ComponentCondition.good && age < lifespan) {
          if (age >= 0 && age <= i) {
            print('bestconditionPrice');
            return (scrapValue * 3).toInt();
          } 
          
          if (age > i && age <= i * 2) {
            return (scrapValue * 2).toInt();
          } 
          
          if (age > i * 2 && age < lifespan) {
            return (scrapValue * 1.5).toInt();
          }
        } 
        
        if ((age >= lifespan) || condition == ComponentCondition.damaged) {
          return scrapValue.toInt();
        }
        
        return 0;
      }
    
      void updateCondition(ComponentCondition partCondition) {
        condition = partCondition;
      }
    }
    
    void main(List<String> args) {
      final engine = ComponentModel(componentPercent: 18, kerbWeight: 695, rate: 17, lifespan: 13, modelMakeYear: 2021);
      engine.updateCondition(ComponentCondition.good);
      final price = engine.calculatePrice();
      final price2 = engine.calculatePrice();
      print('price1 :$price');
      print('price2 : $price2');
    }
    

    像魅力一样工作:

    最佳条件价格 最佳条件价格 价格1:6380 价格2:6380

    这里重要的不是鱼,而是钓鱼方法:遵守所有最佳实践并让您的编译器完成繁重的工作。这是一个很棒的工具,使用它

    【讨论】:

    • 这个答案在很多方面对我很有帮助,我让整个项目变得如此混乱,并且有很多样板代码,但这给了我改进我的代码并优化它的想法,非常感谢先生。
    【解决方案2】:

    您正在更新类中的 age 变量。因此,第二次计算将基于不同的年龄和差异。

    在 calculatePrice() 函数中创建一个局部年龄变量,并基于该变量执行计算以保持一致性

    【讨论】:

    • 谢谢你的回答我被困在这个愚蠢的操作上几个小时再次感谢:)
    • 乐于助人:)
    猜你喜欢
    • 1970-01-01
    • 2013-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-27
    • 2023-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多