The error is caused by the null safety feature in Dart, see https://dart.dev/null-safety.
The result of method stdin.readLineSync()
is String?
, i.e. it may be a String
, or it may be null
. The method int.parse()
requires a (non-null) String
. You should check that the user gave some input, then you can assert that the value is non-null using birthyear!
. Better still, you should use int.tryParse()
to check that the user input is a valid integer, for example:
var birthyearint = int.tryParse(birthyear ?? "");
if (birthyearint == null) {
print("bad year");
} else {
var age = 2021-birthyearint;
print(age);
}