I have this simple calculator made in Flutter, my problem is I want to check if user inputted a string in a Text Field then (if true) display "Must enter valid number". How can I do this? I'm new to flutter so can someone help me?
I'm aware in using keyboard type as a number but that's not my task. I'm also aware for Text Field validation but that's not what I want, I need to display error message using a Text Widget.
These are my variables and controller
final TextEditingController fController = new TextEditingController();
final TextEditingController sController = new TextEditingController();
String errorMessage = "None";
String result = '0';
double num1, num2;
This is my addNum
void addNum() {
setState(() {
num1 = double.parse(fController.text);
num2 = double.parse(sController.text);
result = (num1 + num2).toString();
});
}
This is the onPressed
onPressed: () {
addNum();
if (result.contains('a') || result.contains('a')) {
setState(() {
errorMessage = "Invalid";
});
}
},
This is where I display the text
Text(
"Output: ${result.toString()}\n"
"Error: ${errorMessage.toString()}",
),
I'm getting an error "Invalid Double a".