Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

LateInitializationError: Field '_amount@25125456' has not been initialized

I am trying to do some tax calculation in my flutter project but, It is giving me two errors.

  1. LateInitializationError: Field ‘_amount@25125456’ has not been initialized.
  2. 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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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');
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading