'A dart code behaves differently in dartpad and local because of double type

//Works on Dartpad

main() {
  var pi = 3.14; //3.14159265359
  var numbers = 0;
  dynamic result = 1.2;

  while (result.runtimeType == double) {
    numbers++;
    result = numbers * pi;
  }
  print('$result / $numbers = $pi');
}

//Works on local

main() {
  var pi = 3.14; //3.14159265359
  var numbers = 0;
  dynamic result = 1.2;
  while ((result - result.truncate()) != 0) {
    numbers++;
    result = numbers * pi;
  }
  print('${result.truncate()} / $numbers = $pi');
}

The problem is whenever you initialize a double variable, the dartpad can convert it to an integer when it becomes an integer but the local compiler doesn't do that. Could this be caused by js? Because as far as I know dartpad compiles with js.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source