-4
import "dart:math";
import "dart:io";

void main(){
  print("enter first number");
  double num1 = double.parse(stdin.readLineSync());
  print("enter second number");
  double num2 = double.parse((stdin.readLineSync());

  print (
    num1 + num2
        );
  
}
  

Why it doesnt compile

Enter two numbers from the user add them and the program returns their sum.

Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
Jsent
  • 9
  • 1

2 Answers2

1

For double.parse it needs a String. But stdin.readLineSync() provides nullable String, means this method can return null or string. It is safe to use .tryParse and provide default String instead of using !.

double num1 = double.tryParse(stdin.readLineSync() ?? "") ?? 0;
print("enter second number");
double num2 = double.tryParse((stdin.readLineSync() ?? "")) ?? 0;

print(num1 + num2);

More about tryParse and understanding-null-safety

Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
0

There are 2 issues. First, double.parse requires a String, but readLineSync returns a String?. To solve, simply convert it by using the ! operator, like

 double num1 = double.parse(stdin.readLineSync()!);

The second issue is that you have a ( too many at the num2 = double.parse(( part, so change

double num2 = double.parse((stdin.readLineSync());

to

double num2 = double.parse(stdin.readLineSync()!);
Ivo
  • 18,659
  • 2
  • 23
  • 35