flutter简单计算函数错误

flutter simple calculation Function error

当我调用该函数一次时,它 运行 完美,但如果我再次调用它,它将显示不同的值并读取不同的 else 块,请检查我附加的整个代码 class以及以下函数的驱动程序代码和输出 我认为 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");
 
}

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

在calculatePrice()函数中创建一个本地年龄变量,并根据该变量进行计算以保持一致性

首先,这是一个问得很好的问题,您提供了一个完美可重现的示例,恭喜。

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

  • 使用最新版本的Flutter。它支持空安全。
  • 更多地使用final关键字。每个你不希望改变的变量,使它们成为 final。这样,当您 不小心更改它们时,您的编译器会抛出错误,您会立即注意到。
  • 初始化所有数据。空安全会在这里帮助你,因为当你忘记时你会再次遇到编译错误。
  • 不要保留实例数据,因为你可以。如果某物可以成为局部变量,使成为局部变量。
  • 确保方法简短明了,所以当您可以简单地 return 并完成它时,不要进入无穷无尽的 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');
} 

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

Error: The setter 'age' isn't defined for the class 'ComponentModel'.

  • 'ComponentModel' is from 'package:dartpad_sample/main.dart' ('lib/main.dart'). age = DateTime.now().year - age; ^^^ Error: Compilation failed.

很棒。编译器刚刚发现了你的错误。您不小心重新分配了 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');
}

很有魅力:

bestconditionPrice bestconditionPrice price1 :6380 price2 : 6380

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