'A value of type 'num' can't be assigned to a variable of type 'int'

I am getting error : "A value of type 'num' can't be assigned to a variable of type 'int'." while executing this code. I got the fix. Just wanted to know why this error. Also Do we need to add "!" for every variable operation?

void main() {  
  Map <int, int> val = {1:10};
  int k = val[1]+5;
  //int k = val[1]!+5; //fix
}

o/p dart pad:

Error compiling to JavaScript:
Info: Compiling with sound null safety
Warning: Interpreting this as package URI, 'package:dartpad_sample/main.dart'.
lib/main.dart:3:17:
Error: Operator '+' cannot be called on 'int?' because it is potentially null.
  int k = val[1]+5;
                ^
lib/main.dart:3:17:
Error: A value of type 'num' can't be assigned to a variable of type 'int'.
  int k = val[1]+5;
                ^
Error: Compilation failed.



Solution 1:[1]

Just put a not null operator (!) in line 3 as shown

void main() {  
  Map <int, int> val = {1:10};
  int k = val[1]!+5;
  print(k);
}

no issue was raised during execution, please lmk if the issue is still faced. enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Delwinn