Home > Mobile >  Flutter - The Method '*' was called on Null
Flutter - The Method '*' was called on Null

Time:05-28

Working on a crypto project, I am getting errors in my widget, the cause of the error is the getBalance method below

 String getBalance() {
    String _unit = getUnit(); // it returns a string 
    return boolFrom
        ? (balances[_unit]).toStringAsFixed(4)
        : ((balances[_unit] * rates[_unit]) ?? 0).toStringAsFixed(2);
  }

When I tried calling getBalance onInit State

 @override
  void initState() {
    super.initState();
    print('Get Balance is ${getBalance()}');
  }

I get the error NoSuchMethodError: The method '*' was called on null

CodePudding user response:

Make sure that balances[_unit] and rates[_unit] have a not null value.

CodePudding user response:

Please update your code with below.

String getBalance() {
    String _unit = getUnit(); // it returns a string 
    return boolFrom
        ? ((balances[_unit] ?? 0)).toStringAsFixed(4)
        : (((balances[_unit] ?? 0) * (rates[_unit]) ?? 0)).toStringAsFixed(2);
  }
  • Related