The method 'toStringAsFixed' can't be unconditionally invoked because the receiver can be 'null'.
amount.toStringAsFixed(2)
while accessing amount double value with "toStringAsFixed(2)" the result have to be a a double with decimal rounded to 2 digit.
CodePudding user response:
It means amount
can be null so you can't always do it. Dart has null safety in its language. Meaning it prevents code to compile when it detects that you are using a method on an object that might be null. Learn more about it here: https://dart.dev/null-safety/understanding-null-safety
So solve it there are a couple of posibilties. If you are 100% sure that it can't be null at that line of code you can write
amount!.toStringAsFixed(2)
With the !
you indicate that you are sure it's not null. Note that your program will crash when it does happen to be null.
You can also write
amount?.toStringAsFixed(2)
This is basically the same as
amount != null ? amount.toStringAsFixed(2) : null
So, when amount is null it returns null, otherwise the method you want.
The safest solution is probably to write it like
amount?.toStringAsFixed(2) ?? ""
Whatever you write after the ??
will act as a fallback value. It returns this when amount is null
CodePudding user response:
can be fixed using Null-aware operator ?
amount?.toStringAsFixed(2)