Flutter ● ● ●

Kodebits Day 56: Factory Constructor

Jul 12 2026
Practice factory constructors with a short dart challenge.

What does this print?

class Box {
  final int val;
  Box._(this.val);
  factory Box(int n) =>
    Box._(n * 2);
}

void main() { 
  print(Box(3).val);
}


Try it in the online Dart Playground →

[spoiler title="Solution"]

Answer:

6

Explanation:

The factory constructor Box(int n) processes the input value before passing it to the private constructor Box._().

[/spoiler]


Further Reading