1
int item = int.parse(stdin.readLineSync());

"Error: The argument type 'String?' can't be assigned to the parameter type 'String' because 'String?' is nullable and 'String' isn't."

I didn't know what this want to mean, I just want to convert a string that I will type into integer

I'm using VScode with Dart extension

Thanks for your attention

Cl22
  • 13
  • 4

2 Answers2

0

The error is caused by the null safety feature in Dart, see https://dart.dev/null-safety.

int.Parse() requires a non-null string. While std.readLineSync() is of a nullable string type.

If you check that the user gave input, you can confirm that the value is not null.

Something like:

if(item ==null){
    print("empty input not allowed");
}
else{
    int item = int.parse(stdin.ReadLineSync());
    print(item);
}

You can also use tryParse instead.

var itemInt = int.tryParse(item ?? "");
Corey Sutton
  • 767
  • 5
  • 19
0

Use this

 int item = int.parse(stdin.readLineSync()??"0");

instead of this

int item = int.parse(stdin.readLineSync());

enter image description here

here in the image you can see readLineSync return String? not String so the chance of reading input string may be non string value or emptyline .so we cant convert to number.

lava
  • 6,020
  • 2
  • 31
  • 28