dartoperatorsdart-io

How i can convert to double .dart


I have problem in this code, this code can not be executed. Here is the code:

import 'dart:io';

main() {
  print("CALCULATOR");

  stdout.write("Number a: ");
  double a = double.parse(stdin.readLineSync());
  stdout.write("Number b: ");
  double b = double.parse(stdin.readLineSync());

  double result;

  // operator +
  result = a + b;
  print("$a + $b = $result");

  // operator -
  result = a - b;
  print("$a - $b = $result");

  // operator *
  result = a * b;
  print("$a * $b = $hasil");

  // operator /
  result = a / b;
  print("$a / $b = $result");

  // operator %
  result = a % b;
  print("$a % $b = $result");

}

This my problem:

myLearning/myNote/test.dart:7:33: Error: The argument type 'String?' can't be assigned to the parameter type 'String' because 'String?' is nullable and 'String' isn't.
double a = double.parse(stdin.readLineSync());
                            ^
myLearning/myNote/test.dart:9:33: Error: The argument type 'String?' can't be assigned to the parameter type 'String' because 'String?' is nullable and 'String' isn't.
double b = double.parse(stdin.readLineSync());

I just want if I input a number the result will be like this:

CALCULATOR
Number a : 9
Number b : 2
9.0 + 2.0 = 11.0
9.0 - 2.0 = 7.0
9.0 * 2.0 = 18.0
9.0 / 2.0 = 4.5
9.0 % 2.0 = 1.0

can I get input like the one above?


Solution

  • Your "problem" is that you are using a beta version of Dart 2.12 which enables the non-nullable feature. This feature will help you ensure you are aware of all situations where a null value can happen. In your case it is the call to stdin.readLineSync():

    Returns null if no bytes preceded the end of input.

    You are getting an error because double.parse takes a String as argument but stdin.readLineSync() are returning String? which are a type which can be a String or null (where String only allows String and can never be null).

    The easy fix is to do the following and inserting ! after readLineSync():

    import 'dart:io';
    
    void main() {
      print("CALCULATOR");
    
      stdout.write("Number a: ");
      double a = double.parse(stdin.readLineSync()!);
      stdout.write("Number b: ");
      double b = double.parse(stdin.readLineSync()!);
    
      double result;
    
      // operator +
      result = a + b;
      print("$a + $b = $result");
    
      // operator -
      result = a - b;
      print("$a - $b = $result");
    
      // operator *
      result = a * b;
      print("$a * $b = $result");
    
      // operator /
      result = a / b;
      print("$a / $b = $result");
    
      // operator %
      result = a % b;
      print("$a % $b = $result");
    }
    

    By doing ! you are telling Dart that you are sure the value is not null and the analyzer should just accept the value as if it is never null.

    On runtime, Dart will add a runtime check for null and crash the program (with a NullPointerException) if the returned value from stdin.readLineSync() is null. But since we are working on a small example program, this is properly fine.

    You can read more about non-null by default (NNBD) here: https://dart.dev/null-safety