Flutter - 在 Null 上调用了方法“*”

Flutter - The Method '*' was called on Null

在加密项目上工作,我的小部件出现错误,错误原因是下面的getBalance method

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

当我尝试调用 getBalance onInit State 时

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

我收到错误 NoSuchMethodError: The method '*' was called on null

确保 balances[_unit] 和 rates[_unit] 具有非空值。

请使用以下内容更新您的代码。

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