void printSum({int x = 5, int y = 6, int? k}) {
int sum = (k != null) ? (x + y) : (x + y + k);
print(sum);
}
Error: Getting error on :(x + y + k) for variable k
"message": "The argument type ‘int?’ can’t be assigned to the parameter type ‘num’. ",
>Solution :
this is due to the fact that k is nullable (int?), and the ternary expression is trying to use k in an arithmeticc operation where a non-nullable integer is expected.
you should provide a default value for k when it is null or handle the null case properly. One way to do this is by using the null-coalescing operator (??) to provide a default value for k.
void printSum({int x = 5, int y = 6, int? k}) {
int sum = (k == null) ? (x + y) : (x + y + k);
print(sum);
}