1

I recently got into flutter and dart and I've been writing basic programs. Just this morning I ran into an error when trying to convert user input from a string into an integer so I could perform a mathematical operation. Here's the code.

import "dart:io";
import "dart:math";
import "dart:convert";

void main() {
  print("Enter a number: ");
  String num1 = stdin.readLineSync();

  print("Enter a second number");

  String num2 = stdin.readLineSync();

  print(int.parse(num1) + int.parse(num2));
}

Surprisingly this runs well on the online dart compiler and interpreter. (replit)

but when I run it on vscode, I get this error

"Error: A value of type 'String?' can't be assigned to a variable of type 'String' because 'String?' is nullable and 'String' isn't.
  String num2 = stdin.readLineSync();"

Whichever data type I use, I get the same error. be it var, or string, or int or double.

I tried another method of conversion but its the same thing. works on the online compiler, doesn't work on my machine. here's the code.

import "dart:io";
import "dart:math";
import "dart:convert";

void main() {
  print("Enter a number: ");
  var num1 = stdin.readLineSync();
  var num2 = int.parse(num1);

  print("Enter a second number");

  var num3 = stdin.readLineSync();
  var num4 = int.parse(num3);

  print(num2 + num4);
}
Zahra
  • 2,231
  • 3
  • 21
  • 41

1 Answers1

2

stdin.readLineSync() returns a String?. Types ending in a ? are nullable. String? means either String or null.

Also int.parse takes a parameter of type String not String? so you cannot pass a value to it which might be null. There are a few different approaches you can take to work around this. One thing you can do is check if they are not null in an if statement, and in doing so the values of num1 and num2 will be promoted to String within the body of the if statement. Another approach is to use the ?? operator on num1 and num2, which is called the "if null" operator and allows you to provide a default value in the event that num1 or num2 is null.

import "dart:io";
import "dart:math";
import "dart:convert";

void main() {
  print("Enter a number: ");
  // set type to String? (you can also use var or final)
  String? num1 = stdin.readLineSync(); 

  print("Enter a second number");

  String? num2 = stdin.readLineSync();
  
  // check for null values before using num1 and num2
  if (num1 != null && num2 != null) {
    print(int.parse(num1) + int.parse(num2));
  }

  // alternatively you can convert the values to String 
  // using the ?? operator. if the value of num1 is null
  // then you will get '' (empty string) instead.
  print(int.parse(num1 ?? '') + int.parse(num2 ?? ''));
}

I am not familiar with replit, but it may be using an older version of dart language. dart version 2.12 introduced features related to null safety.

See also:

https://medium.com/dartlang/announcing-dart-2-12-499a6e689c87

https://dart.dev/null-safety/understanding-null-safety

https://dart.dev/codelabs/null-safety

mmcdon20
  • 5,767
  • 1
  • 18
  • 26