'When I pressed setState function, numbers are not adding

On numbers are not adding when I press the calculate button

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,
        ),
      ),

Please help



Solution 1:[1]

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

Solution 2:[2]

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/?

Solution 3:[3]

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?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Grigoris Kritopoulos
Solution 2 Kumaresan Jackie
Solution 3 TaigaNatto