How do use a Switch Case Statement in Dart

The comparsion of double values using ‘==’ is not very reliable and should be avoided (not only in Dart but in most languages).

You could do something like

methodname(num radians) {
  // you can adjust this values according to your accuracy requirements
  const myPI = 3142; 
  int r = (radians * 1000).round();

  switch (r) {
    case 0:
      // do something
      break;
    case myPI: 
      // do something else
      break;
  }
}

This question contains some additional information that might interest you

  • comparing float/double values using == operator
  • How should I do floating point comparison?

some more information:

  • https://www.dartlang.org/docs/spec/latest/dart-language-specification.html#h.50ae78s6gbw2
  • http://floating-point-gui.de/errors/comparison/

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)