skip to Main Content

I tried to add the text as below to make it work but it just shows =(sum of round 1) +(sum of round 2) but doesnt add them up: CODE

            Text(
              "Result n Round 1: $roundOneResult and Round 2: $roundTwoResult Total = $roundOneResult + $roundTwoResult",
              textAlign: TextAlign.center,
              style: const TextStyle(
                fontSize: 30,
              ),
            ),
          ],
        ),
      ),
    );
  }
}

I tried the above and was expectingenter image description here

more code sample if you need a reference thanks all:

            mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                ElevatedButton(
                  child: const Text("Add"),
                  onPressed: () {
                    setState(() {
                      roundOneFields.forEach((e) =>
                          roundOneResult = roundOneResult + int.parse(e.text));
                      roundTwoFields.forEach((e) => roundTwoResult =
                          roundTwoResult + int.parse(e.text.toString()));
                    });
                  },
                )
              ],
            ),
            Text(
              "Result n Round 1: $roundOneResult and Round 2: $roundTwoResult Total = $roundOneResult + $roundTwoResult",
              textAlign: TextAlign.center,
              style: const TextStyle(
                fontSize: 30,
              ),
            ),
          ],
        ),
      ),
    );
  }
}

2

Answers


  1. this problem arose because you added val 1 and val 2 with ‘+’ which is working as a String. try this:

                    Text(
                    "Result n Round 1: $roundOneResult and Round 2: 
                     $roundTwoResult Total = ${roundOneResult + 
                     roundTwoResult}",
                     textAlign: TextAlign.center,
                     style: const TextStyle(
                     fontSize: 30,
                    ),
                  ),
    

    if. it’s worked then mark it as solved.

    Login or Signup to reply.
  2. The other answer is also correct. Anotherway,

    // Calculate the total sum
    double totalSum = roundOneResult + roundTwoResult;
    
    // ...
    
    Text(
      "Result n Round 1: $roundOneResult and Round 2: $roundTwoResult Total = $totalSum",
      textAlign: TextAlign.center,
      style: const TextStyle(
        fontSize: 30,
      ),
    ),
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search