Default Dart code provided in Onboarding throws error

when I played the Onboarding puzzle with Dart, this provided code did not work.

    String enemy1 = stdin.readLineSync();
    int dist1 = int.parse(stdin.readLineSync());
    String enemy2 = stdin.readLineSync();
    int dist2 = int.parse(stdin.readLineSync());

error output:

Answer.dart:7:27: 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 enemy1 = stdin.readLineSync();

adding a ! (null check) after the readLineSync() solved the issue:

    String enemy1 = stdin.readLineSync()!; 
    int dist1 = int.parse(stdin.readLineSync()!); 
    String enemy2 = stdin.readLineSync()!; 
    int dist2 = int.parse(stdin.readLineSync()!); 

or, pressing the “Reset” button and then replacing the output would work, too.
could you fix it? I think it’s hard for the beginners to find out…

1 Like

Thank you for reporting this. We have updated the solutions to fit Dart’s new readLineSync signature.

2 Likes