How to Type Cast in Flutter

Dart is a typesafe language. While it is very nice and safe, sometimes - specially coming form other languages where type is not that important, it tends to create headache.

‘AS’ to give a hint

AS keyword gives a hint to compiler. We can only use this if the type of the object is a defined subtype, otherwise you cannot use it.

In the code above, if you remove as Honda you will notice that the code doesn’t compile anymore. Compiler does not know that the object is Honda.

That said, remember that Dart compiler is pretty smart. Most of the time, it knows what is the type of the object.