Types

If you had heard that Dart was a statically typed language, you might be very confused at this point. None of the code that we have written so far declares a single type. Thanks to copious use of the var keyword, our code is blissfully type-free. So what’s the deal?

Dart is not, in reality, statically typed. It is sneakily typed. To understand what that means, consider the following code.

primitives/types.dart
 
var​ muppet = ​'Piggy'​;
 
 
// Dart, like JavaScript, allows this, but come on!
 
muppet = 42;

We can infer that var indicates a variable type. In other words, not only are we not specifying a type, but we are telling the interpreter that the type can change. So what happens when we declare muppet to have String type?

 
String ...

Get Dart 1 for Everyone now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.