首页 > 解决方案 > 为什么在 if 语句中取不到值?

问题描述

Widget build(BuildContext context) {
final bmi = weight / ((height / 100) * (height / 100));
final bmi = weight / ((height / 100) * (height / 100));
if(sex == 1){
  final base = 66.47+(13.75*weight)+(5*height)-(6.76*age);
  if(exercise == 0){
    final active = base * 0.2;
  }else if(exercise <= 3){
    final active = base * 0.375;
  }else if(exercise <= 5){
    final active = base * 0.555;
  }else{
    final active = base * 0.8;
  }
}else if(sex == 2){
  final base = 65.51+(9.56*weight)+(1.85*height)-(4.68*age);
  if(exercise == 0){
    final active = base * 0.2;
  }else if(exercise <= 3){
    final active = base * 0.375;
  }else if(exercise <= 5){
    final active = base * 0.555;
  }else if(exercise >= 6){
    final active = base * 0.8;
  }
}
return Scaffold(
  appBar: AppBar(title: Text('Result')),
  body: Center(
    child: Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        Text(
          _calcBmi(bmi),
          style: TextStyle(fontSize: 30),
        ),
        SizedBox(
          height: 16,
        ),
        _buildIcon(bmi),
        SizedBox(
          height: 10,
        ),
        Text(
          _calcBase(base),
          style: TextStyle(fontSize: 15),
        ),
        SizedBox(
          height: 10,
        ),
        Text(
          _calcActive(active),
          style: TextStyle(fontSize: 15),
        ),

      ],
    ),
  ),
);
}

String _calcBmi(double bmi) {
var result = 'underweight';
if (bmi >= 35) {
  result = 'severe obesity';
} else if (bmi >= 30) {
  result = 'two-stage obesity';
} else if (bmi >= 25) {
  result = 'first-stage obesity';
} else if (bmi >= 23) {
  result = 'overweight';
}else if(bmi>=18.5){
  result='normal';
}
return result;
}

String _calcBase(double base) {
var result = 'basic metabolic rate : $base';
return result;
}

String _calcActive(double active) {
var result = 'active metabolic rate : $active';
return result;
}

我正在尝试制作一个颤振的应用程序,输出 bmi 值,但不输出基本值和活动值。我想知道为什么我不能带来基础和积极的价值。我想根据输入的性别值和运动值以不同的方式打印出基本和活性代谢物。我不能在条件语句中得到最终值吗?

标签: androidflutter

解决方案


您在 if 范围内声明 base 和 active,它在外部不存在。

尝试:

Widget build(BuildContext context) {
final bmi = weight / ((height / 100) * (height / 100));
final bmi = weight / ((height / 100) * (height / 100));

var base, active;

if(sex == 1){

然后在基础和活动之前删除每个决赛。


推荐阅读