Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

The argument type 'int?' can't be assigned to the parameter type 'num'. Dart & Flutter

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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);
    }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading