Given there is something along this lines:

```
class Foo {}
class Bar extends Foo {} 
class Baz extends Foo {} 

```

I find myself writing this type of code:

```
if (foo is Bar) {
   (foo as Bar).doSomething();    //Compiler warning of unnecessary cast.
} else (foo is Baz) {
   (foo as Bar).doSomething();    //Compiler warning of unnecessary cast.
}

```

I don't know how to avoid this situation.

If I remove the check types of of **is Bar** or **is Baz** before the cast I may get a runtime error and if I don't cast, it means no access to the public stuff on that type.

Maybe I follow a flawed code design that I need to update because I believe I should avoid checking class type.

Is there any method in Dart that may help here?

`(foo as? Bar)?.doSomething()` (swift like)