flutter calculator – how to show an error msg when user divide by zero

0

Issue

I’m trying to validate that the second number is not zero only when user try to divide
my code

Solution

onPressed: () {
  if (int.parse(secondNumberController.text) == 0) {
    // show error message here
    ScaffoldMessenger.of(context).showSnackBar(
      const SnackBar(
        content: Text('second num should not be zero!'),
      ),
    );
    return;
  }
  setState(() {
    double divide = int.parse(firstNumberController.text) /
        int.parse(secondNumberController.text);
    result = divide.toString();
  });
},

Answered By – 聂超群

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More