Dart Flutter Tips

Callable classes

A Callable class allows its instance to be called a function. This feature of Dart language helps make named functions.
Plague Fox
Callable classes
Photo by Tony Pepe / Unsplash

A Callable class allows its instance to be called a function. This feature of Dart language helps make named functions.

You can use any class as a function. Just add a method call to it.

void main() {
  final mult = Multiplication(10);
  print(mult(2)); // 20
  print(mult(5)); // 50
  print(mult(-10)); // -100
}

class Multiplication {
  Multiplication(this.value);

  final num value;

  num call(num factor) => value * factor;
}
Share
Comments
More from Plague Fox
Microbenchmarks are experiments
Dart Flutter Article

Microbenchmarks are experiments

Benchmarks are not just about numbersโ€”they are experiments that need interpretation. This post dissects a Dart vs JavaScript microbenchmark, illustrating why cool animations often mask the real value: insightful analysis. Numbers without context are just as meaningful as numerology
Vyacheslav Egorov 12 min read

Plague Fox

Engineer by day, fox by night. Talks about Flutter & Dart.

Great! Youโ€™ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Plague Fox.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.