Challenge - 5 Problems
Variables and Type Inference Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
📝 Syntax
intermediate1:30remaining
Understanding Dart variable type inference
What is the type of variable
x after this code runs?var x = 42;
Flutter
var x = 42;Attempts:
2 left
💡 Hint
Dart infers the type from the assigned value.
✗ Incorrect
The variable
x is assigned the integer 42, so Dart infers its type as int.❓ ui_behavior
intermediate1:30remaining
Effect of variable type on Flutter widget display
Given this Flutter code snippet, what will be displayed on the screen?
var message = 123; return Text(message.toString());
Flutter
var message = 123; return Text(message.toString());
Attempts:
2 left
💡 Hint
Check how the integer is converted to text.
✗ Incorrect
The integer 123 is converted to a string using
toString(), so the Text widget displays '123'.❓ lifecycle
advanced2:00remaining
Variable type and widget rebuild behavior
In a Flutter StatefulWidget, if you declare a variable with
var count = 0; inside the State class and update it inside setState(), what happens to the variable type during rebuilds?Flutter
class _MyWidgetState extends State<MyWidget> { var count = 0; void increment() { setState(() { count++; }); } }
Attempts:
2 left
💡 Hint
Consider where the variable is declared and how Flutter rebuilds widgets.
✗ Incorrect
The variable
count is a field of the State class and keeps its type int and value across rebuilds.🔧 Debug
advanced2:00remaining
Identifying type inference error in Dart
What error will this Dart code produce?
var name = null; name = "Flutter";
Flutter
var name = null; name = "Flutter";
Attempts:
2 left
💡 Hint
Think about what type Dart infers when initialized with null.
✗ Incorrect
Dart infers the type as
Null when initialized with null, so assigning a String later causes a compile-time error.🧠 Conceptual
expert2:00remaining
Why use explicit types instead of var in Flutter?
Which reason best explains why you might prefer explicit types over
var in Flutter app variables?Attempts:
2 left
💡 Hint
Think about how explicit types help developers understand code.
✗ Incorrect
Explicit types make code clearer and help the compiler catch mistakes before running the app.