-1

Every time i need to input certain variable as integer i used to use this below code for that

import 'dart:io';

void main(List<String> arguments) {
int a = 0;
        print("Enter a :");
        String? x = stdin.readLineSync();
        if (x != null) {
          a = int.parse(x);
        }
}

which is very hectic...very since the null safety was added from Dart 2.12 version.Before it the integer was inputted using this code int n = int.parse(stdin.readLineSync()); can anyone propose to make it smaller...since its hefty..

REtr0
  • 17
  • 2
  • 6
  • The code is hefty now for a reason. The prior method hid a dangerous potential for a null reference exception in the case that `stdin.readLineSync` returned null. With null safety, that method is now clear that it has the potential to return null so you are forced to handle that possibility (or at least explicitly say you don't _want_ to handle it with the `!` operator). You've taken a small step back in code brevity but taken five large steps forward with code safety. – Abion47 Jun 25 '21 at 23:00

1 Answers1

0

Since we are using Null Safety by default with Dart 2.12.2. We also know that int.parse() take a String that cannot be null due to Null Safety. however int.parse(readLineSync()) can return a int? which is a nullable value by simply adding the The null assertion operator (!) at the end of stdin.readLineSync() as show in code below

import 'dart:io';
    
    void main(List<String> arguments) {
    
      print("Enter a :");
      int? a = int.parse(stdin.readLineSync()!);
    }

The null assertion operator (!)

If you’re sure that an expression with a nullable type isn’t null, you can use a null assertion operator (!) to make Dart treat it as non-nullable. By adding ! just after the expression, you tell Dart that the value won’t be null, and that it’s safe to assign it to a non-nullable variable.

note:-If you’re wrong, Dart throws an exception at run-time. This makes the ! operator unsafe, so don’t use it unless you’re very sure that the expression isn’t null.

Hope you find this useful...(☞゚ヮ゚)☞

REtr0
  • 17
  • 2
  • 6
  • There's a reason why `stdin.readLineSync()` returns a nullable type! Since you're reading *user* input, there's no way that you can be sure that `stdin.readLineSync()` won't return `null`. This will throw an uncaught exception if the user enters EOF. You *must* check for `null`. Also see https://stackoverflow.com/a/66965671/. – jamesdlin Jun 25 '21 at 21:04
  • Thanks for the info @jamesdlin........but we can use it for simple programs as long it's running i suppose..thats why i added note at end... – REtr0 Jun 25 '21 at 21:16
  • 1
    @REtr0 Practice doesn't make perfect. _Perfect_ practice makes perfect, and getting in the habit of using `!` when you just don't want to deal with nulls is not a good habit to develop. I'll be the first to admit that I don't always adhere to best practices when I'm writing simple scripts, but I would never _recommend_ someone use bad practices just because it's a simple script. – Abion47 Jun 25 '21 at 23:03