Type Inference vs Explicit Types
When to annotate and when to let Dart decide.
Type Inference vs Explicit Types is a free Dart Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Dart Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Two Ways to Declare
You can write the type yourself or let Dart guess it. Both produce the same strongly typed variable underneath.
Explicit Types Spell It Out
An explicit declaration names the type up front. It is clear and self-documenting for anyone reading your code.
int total = 42;Type Inference Lets Dart Decide
With var, Dart infers the type from the value on the right. Less typing, same safety, since the type is still fixed.
var total = 42; // inferred intInference Is Not dynamic
Inferred does not mean loose. A var still gets one concrete type and rejects mismatched assignments later.
var name = "Sam";
// name = 5; // errorWhen to Be Explicit
Spell out the type when the value alone is ambiguous, like an empty list, so readers know exactly what it holds.
List<int> scores = [];Inference Reads the Literal
Dart looks at the literal you wrote: a dot means double, no dot means int. The literal shape drives the choice.
var x = 5; // int
var y = 5.0; // doubleFunction Returns Get Inferred Too
When you call a function, var infers the variable type from that function return type automatically.
var s = "hi".toUpperCase(); // StringExplicit Types Catch Mistakes
Declaring an explicit type lets Dart flag a wrong value immediately, before it ever runs.
int count = "ten"; // compile errorReadability Versus Brevity
There is a tradeoff: explicit types add clarity, while inference keeps lines short. Favor clarity in shared code.
Both Are Statically Typed
Whichever you choose, Dart is statically typed. The type is locked at compile time, not changed while running.
A Practical Rule of Thumb
Let Dart infer when the value is obvious, and write the type when it adds meaning. Aim for code that reads itself.
Quick Check
What does var actually do to the type of a variable?
Recap: Inferred vs Explicit
You saw that explicit types document intent while inference keeps code short. Both stay statically typed and safe. 👍
Frequently asked questions
Is the “Type Inference vs Explicit Types” lesson free?
Yes — the full text of “Type Inference vs Explicit Types” is free to read here on the web, and the Dart Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Dart Academy course, upgrade to CoddyKit PRO.
What will I learn in “Type Inference vs Explicit Types”?
When to annotate and when to let Dart decide. You practise Dart Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Dart Academy?
No prior experience is required. Dart Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Type Inference vs Explicit Types” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Dart Academy lesson?
Yes. Every Dart Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- var, final, and const Explained
- The Core Types: int, double, String, bool
- Type Inference vs Explicit Types
- The dynamic and Object Types