I am trying to do some tax calculation in my flutter project but, It is giving me two errors.
- LateInitializationError: Field ‘_amount@25125456’ has not been initialized.
- Error: The operator ‘~/’ isn’t defined for the class ‘String’.
Try correcting the operator to an existing operator, or defining a ‘~/’ operator.
I am not sure why it is happening can anyone please help me out?
// this is my function
showincomeafterTax() {
if (_amount!.isNotEmpty) {
taxamount = (_amount * 17 ~/ 100).toString();
print(taxamount);
} else {
print('Insert an amount');
}
}
// designing part
Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15.0),
),
child: Column(
children: <Widget>[
Padding(
padding: EdgeInsets.all(10.0),
),
Container(
child: Text(
'Income after Tax',
style: TextStyle(
fontWeight: FontWeight.bold,
),
),
),
Container(
child: Column(
children: <Widget>[
ListTile(
leading: Icon(Icons.monetization_on_outlined),
title: showincomeafterTax(),
),
],
),
),
],
),
),
>Solution :
Your error is kinda clear:
Error 1. Your variable _amount has not been initialized. You may declare you variable as
late String _amount;
But you hasn’t assign/initiate it to any value, therefore your program gives the first error.
Error 2. _amount is String type, you cannot use it in a math like a number. I notice that you use operator ~/, which is used in integer. So you need to convert String to a int, example:
if (_amount!.isNotEmpty) {
int amountDouble = int.parse(_amount);
taxamount = (amountDouble * 17 ~/ 100).toString();
print(taxamount);
} else {
print('Insert an amount');
}