Search⌘ K
AI Features

Type Inference and Annotation

Explore Dart's type system by learning how type inference works using var, how explicit annotations define data boundaries, and how null safety integrates with these features. Understand the role of dynamic types for flexible assignments and when to use each approach for safe, maintainable Dart code.

Dart is a strongly typed, statically typed language. Static typing means that the compiler checks the data types of our variables at compile-time before the code ever runs. This allows the compiler to catch errors early, preventing us from accidentally assigning the wrong kind of data to a variable.

While Dart enforces strict type rules, we do not always have to write the types ourselves. We manage types using a three-pillar mental model:

  1. Explicit types: We manually declare the exact static type (like num or String).

  2. Type inference (var): The compiler automatically determines the static type based on the initial value.

  3. Dynamic typing (dynamic): We disable static checking entirely, allowing the variable to hold any type of data.

Type inference

As the name implies, type inference is the compiler's ability to infer data types when they are not ...