Switching on class type in Dart

You can use the runtimeType in switch :

class Foo {
  Foo getAnother(Foo foo) {
    switch (foo.runtimeType) {
      case Bar:
        return new Bar();
      case Baz:
        return new Baz();
    }
    return null;
  }
}

In the case statements the class name is used directly (AKA class literal). This gives a Type object corresponding to the class mentioned. Thus foo.runtimeType can be compared with the specified type.

Note that you can not use generics for now in class literals. Thus, case List<int>: is not allowed.

Leave a Comment

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