You get the error: "The argument type 'double?' can't be assigned to the parameter type 'double'." Because double.tryParse()
returns nullable double (double?
) "meaning it can be null if parsing fails"
And the parameter is type is non-nullable double (double
) "meaning it always has a value and never can be null".
To fix this you have 2 options:
Choose a value to be used in case of null:
double x = double.tryParse('1.23') ?? 0.0;
Here the double x won't be null even if double.tryParse()
returns null the value 0.0 will be used.
Use null assertion operator (!)
since you checked if enteredStartingMiles == null
and handled that case, in the following lines of code you can say "trust me enteredStartingMiles is not null" by adding (!) at the end of variable name enteredStartingMiles!