0

I am trying to parse an input with the type 'String?' into a double, but it is saying that -

The argument type 'String?' can't be assigned to the parameter type 'String'.

    stdout.write("Enter the first number - ");
    String? vari = stdin.readLineSync();
    var first = int.parse(vari);
noaa
  • 1

1 Answers1

0

Change it to:

int? first = int.tryParse(vari.toString());

This is because String? is nullable, vs String.

  • .parse functions require non-nullable Strings.

  • tryParse returns an int if the string is parsable, and returns null if it can't be parsed. This is why I changed var first to int? first.

  • You can check afterwards if first is null or not. You can also perform this check before parsing it, and your code would look like this:

 String? vari = stdin.readLineSync();
 if (vari != null) var first = int.parse(vari);

This would work.

Huthaifa Muayyad
  • 11,321
  • 3
  • 17
  • 49