-1

I was trying to take integer as input in Dart using the following as it is suggested in many sources.

import 'dart:io';

  void main() {
  int n = int.parse(stdin.readLineSync());
}

But the following error message was shown when I tried to run (Visual Studio Code was also showing it in problems):

Error: The argument type 'String?' can't be assigned to the parameter type 'String' because 'String?' is nullable and 'String' isn't.

Now how can I take a integer or double as input? (I'm using Dart SDK version: 2.12.2 (stable))

Anurag Pathak
  • 15
  • 1
  • 9
  • 1
    Two versions of this question were asked yesterday and today. It is recommended to search using the error message to find previous questions before asking a new one. Then you would find: https://stackoverflow.com/questions/66798782/error-the-argument-type-string-cant-be-assigned-to-the-parameter-type-stri?r=SearchResults&s=1|678.2682 – Patrick O'Hara Mar 29 '21 at 13:13
  • 4
    Does this answer your question? [Error: The argument type 'String?' can't be assigned to the parameter type 'String' because 'String?' is nullable and 'String' isn't](https://stackoverflow.com/questions/66798782/error-the-argument-type-string-cant-be-assigned-to-the-parameter-type-stri) – Patrick O'Hara Mar 29 '21 at 13:16
  • @PatrickO'Hara No. I am still unable to take input as it doesn't suggest how to take input as integer/double. What I need is a way to take input as inetger/double/string. – Anurag Pathak Mar 29 '21 at 13:36
  • I added a comprehensive answer. – Patrick O'Hara Mar 29 '21 at 14:28

2 Answers2

4

You are using Null Safety by default with Dart 2.12.2.

readLineSync() return a String? which is a nullable value.

However, int.parse() take a String that cannot be null due to Null Safety. That's what's the error is about.

In order to fix this, you have to check is readLineSync() return null before using it.

import 'dart:io';

void main() {
  String? s = stdin.readLineSync();
  if (s != null){
    int n = int.parse(s);
    print(n); // Or do whatever you want with your n value
  }
}
Maxouille
  • 2,729
  • 2
  • 19
  • 42
1

An example that handles int, double and String:

import 'dart:io';

void main() {
  String? s = stdin.readLineSync();
  if (s != null) {
    if (int.tryParse(s) != null) {
      int n = int.parse(s);
      print('int $n'); // Or do whatever you want with your int value
    } else if (double.tryParse(s) != null) {
      double d = double.parse(s);
      print('double $d'); // Or do whatever you want with your double value
    } else {
      print('string "$s"'); // Or do whatever you want with your string value
    }
  }
}
Patrick O'Hara
  • 1,999
  • 1
  • 14
  • 18
  • Isn't it possible to take input like we do in C/C++, python etc ? – Anurag Pathak Mar 29 '21 at 15:29
  • Surely the Dart `readLineSync` method is comparable to the C `gets` and the Python `input` functions? I do not know of any Dart equivalent of the C `scanf` function. – Patrick O'Hara Mar 29 '21 at 16:52
  • But in my case when I take input with readLineSync() the format in which input is taken is string? and when I change it to integer using int.parse(), then always error occurs as mentioned in the question and hence I am unable to take input as integer as well as double and string. Only string? type input is possible. – Anurag Pathak Mar 30 '21 at 04:36
  • The method `readLineSync()` returns a string of characters that the user entered, or null, so the type is String?. It is up to your code to interpret the string. You can interpret it as anything you want, the code above parses the string to see if it is an int, or a double, but you could write code to match any sequence of inputs. (The package https://pub.dev/packages/string_scanner can help here.) – Patrick O'Hara Mar 30 '21 at 06:05