How can i take user input in dart?

Viewed 1701
import 'dart:io';
main()
{
  print('What is your name: ');
  String name = stdin.readLineSync();
  print('Your age is = $name');
}

//I'm having this error when i'm taking user input in dart, Error: A value of type 'String?' can't be assigned to a variable of type 'String' because 'String?' is nullable and 'String' isn't.

1 Answers

I guess you are using null safety. The error occurs because stdin.readLineSync() returns a String? but you assing it on String name. To fix it add a ? or a !, but then make sure it is not null.

String? name = stdin.readLineSync();
String name = stdin.readLineSync()!;
Related