Flutter ● ● ○

Kodebits Day 72: Late Variable Init

Aug 8 2026
Practice late variables with a short dart challenge.

What does this print?

int count = 0;
void main() {
  late String msg = getValue();
  print(count);
  print(msg);
  print(count);
}
String getValue() {
  count++;
  return "Hi";
}


Try it in the online Dart Playground →

[spoiler title="Solution"]

Answer:

0
Hi
1

Explanation:

Late variables with initializers use lazy evaluation – getValue() only runs when msg is first accessed, not when declared.

[/spoiler]


Further Reading