0

I'm new at dart. I think that it's about the new dart version that I can't assign String to stdin.readLineSync(). Can Someone Pls tell me the alternatives?

This is just a basic calculator where I'm trying to add two numbers.

import 'dart:io';
import 'dart:math';


void main() {
  print("Enter first number: ");
  String? num1 = stdin.readLineSync();
  print("Enter second number: ");
  String? num2 = stdin.readLineSync();

  print(num1 + num2);

  
}
julemand101
  • 28,470
  • 5
  • 52
  • 48
codegood
  • 29
  • 5
  • Hello, can you add the error message? – Ananda Pramono Dec 10 '21 at 02:33
  • The linked answer is not exact the same problem but very similar since your problem is not with `int.parse` but with using the `+` operator on a `String?` type. But the answer describes in details the problem you are having with nullable types and the different solutions for that. :) – julemand101 Dec 10 '21 at 05:38

1 Answers1

0

This is the alternative way how you do:

In Dart programming language, you can take standard input from the user through the console via the use of .readLineSync() function.

import 'dart:io';

void main()
   {   
    print("Enter first number");
    int? n1 = int.parse(stdin.readLineSync()!);

    print("Enter second number");
    int? n2 = int.parse(stdin.readLineSync()!); 

    int sum = n1 + n2;
    print("Sum is $sum");
   }

and here it is clearly explained about your problem "The argument type 'String?' can't be assigned to the parameter type 'String'" when using stdin.readLineSync()

gretal
  • 1,092
  • 6
  • 14