Since Dart allows someone to define a variable as an int
, String
, etc or as a var
. What's the advantage to defining the type instead of just making the variable a var
every time?
You don't have to use types in Dart programs, but using them has some advantages:
Types act as documentation. People reading your code can understand your intent better if you use types judiciously.
Types can help with tooling. For example, IDEs can provide nifty functionality like method options by using type annotations that you have provided.
Dart provides a static checker, and this uses types to provide early warnings about things you might be doing wrong (Run your programs in checked mode during development. This forces the system to automatically perform all sorts of type checks. If there is an error, you get a clear error message).
You can have use var
everywhere and avoid types if you want. Or you use types extensively as if you were coding in a statically typed language. Generally, though, you will want to take a middle-ground and use types judiciously to make your intent clear.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With