Home > OS >  flutter calculator - how to show an error msg when user divide by zero
flutter calculator - how to show an error msg when user divide by zero

Time:12-06

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

CodePudding user response:

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();
  });
},
  • Related