Killa
Killa

Reputation: 57

Why can't get the value in the if statement?

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;
}

I'm trying to make an app with a flutter, bmi value is output, but base and active value are not output. I want to know why I can't bring base and active value. And I would like to print out basic and active metabolites differently depending on the entered sex value and exercise value. Can't I get the final value in the conditional statement?

Upvotes: 2

Views: 43

Answers (1)

Rodrigo Lima
Rodrigo Lima

Reputation: 103

You are declaring base and active inside the if scope, it doesn't existe outside.

Try:

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

var base, active;

if(sex == 1){

and then remove every final before base and active.

Upvotes: 2

Related Questions