I have created my text field which should only allow number input for the purpose of some computing i will later do after the value has been input. Its almost working fine but there is one issue though.
Here is the code for the field
var numberInputFormatters = [new FilteringTextInputFormatter.allow(RegExp("[0-9]")),];
String _tip='2';
Widget _buildOtherTipInput() {
return TextFormField(
style: inputTextStyle,
inputFormatters: numberInputFormatters,
keyboardType: TextInputType.number,
decoration: formInputDecoration.copyWith(
prefix: Text('\$'),
labelText: 'Enter Custom Tip Amount ',
hintStyle: TextStyle(fontWeight: FontWeight.w600)
),
onChanged: (val){
setState(() {
val!=null?_tip=val:_tip='0';
});
},
);
}
So when you input a number then try to input a non number character, it will not take the input but if i try to input a character that is not a number as the first, i get this error thrown
Invalid double
I do know that it comes from this code below that converts the string input to a double. What i don't understand is why it receives the invalid double in the first place yet i have set blockers for invalid(non number) input in my text field.
String getTotal(){
String theTip=_tip??'0';
double newTip = double.parse(theTip).truncateToDouble();
return newTip.toStringAsFixed(2);
}

FilteringTextInputFormatter.digitsOnlyapi.flutter.dev/flutter/services/FilteringTextInputFormatter/… ?getTotal(). It will be difficult for us to give you any help if the problem resides in another part of your code. Could you please add the relevant part wheregetTotal()is called ?