Home > Software design >  The numbers are not adding. Tap to view more
The numbers are not adding. Tap to view more

Time:04-06

Variable I took

String _bmi = "0";

Here's the code portion

     OutlinedButton(
        child: Text('Calculate',
          style: TextStyle(
            fontWeight: FontWeight.bold,
            color: kTitleTextColor,
          ),
        ),
        onPressed: () {
          setState(() {
            int sum = int.parse(massNumber.text)   int.parse(heightNumber.text);
            _bmi = sum.toString();
          });
        },
      ),
      Text(_bmi,
        style: TextStyle(
          fontWeight: FontWeight.bold,
          color: kTitleTextColor,
          fontSize: 18.0,
        ),
      ),

On numbers are not adding when I press the calculate button. Please help

CodePudding user response:

Your function works fine. I have added a dummy data function below so you can see it. So either you have an error with your data that comes, maybe the are null or empty or not integer or you have another error in your console.

enter image description here

CodePudding user response:

refer this example

void main()

{
  double num = 0;
  String wight = "40";
  String height = "20";
  double sum = double.parse(wight)   double.parse(height);
  num = sum;
  print(num.toString());
}

https://dartpad.dev/?

CodePudding user response:

The code using setState() is working correctly. Try it out.

int sum = int.parse(massNumber.text)   int.parse(heightNumber.text);

is replaced to ↓

int sum = int.parse("1")   int.parse("2");

and run it, it works correctly.

Are the numbers in massNumber.text and heightNumber.text correct?

  • Related