I want to write a function in a Dart superclass that takes different actions depending on which subclass actually uses it. Something like that:
class Foo { Foo getAnother(Foo foo) { var fooType = //some code here to extract fooType from foo; switch (fooType) { case //something about bar here: return new Bar(); case //something about baz here: return new Baz(); } } } class Bar extends Foo {} class Baz extends Foo {}
where the idea is that I have some kind of object and you want to get a new object of the same (sub) class.
The main question is: what type should fooType be? My first thought was Symbol, which leads to statements with light arguments like case #Bar: but I donβt know how I would fill fooType symbol. The only options I can think of is to do something like Symbol fooType = new Symbol(foo.runtimeType.toString()); but I understand that runtimeType.toString() will not work when converting to javascript. You can get around this using Mirrors, but that means it is a small library, so they are not on the table. Object.runtimeType returns something from the Type class, but I have no idea how to create Type instances that I could use for case statements. Maybe I am missing some other part of the Dart library that is better suited for this?
source share