Concept of Callable Classes in Dart
Last Updated :
03 Apr, 2025
Dart allows the user to create a callable class which allows the instance of the class to be called as a function. To allow an instance of your Dart class to be called like a function, implement the call() method.
Syntax :
class class_name {
... // class content
return_type call ( parameters ) {
... // call function content
}
}In the above syntax, we can see that to create a callable class, we have to define a call method with a return type and parameters within it.
Implementing a callable class in Dart
Example :
Dart
// Creating Class GeeksForGeeks
class GeeksForGeeks {
// Defining call method which create
// the class callable
String call(String a, String b, String c)
=> 'Welcome to $a$b$c!';
}
// Main Function
void main() {
// Creating instance of class
var geek_input = GeeksForGeeks();
// Calling the class through its instance
var geek_output = geek_input('Geeks', 'For', 'Geeks');
// Printing the output
print(geek_output);
}
Output:
Welcome to GeeksForGeeks!
Note: It must be noted that Dart doesn't support multiple callable methods i.e. if we try to create more than one callable function for the same class it will display error.
Implementing Multiple callable functions in a class of Dart
Example :
Dart
// Creating Class GeeksForGeeks
class GeeksForGeeks {
// Defining call method which create
// the class callable
String call(String a, String b, String c)
=> 'Welcome to $a$b$c!';
// Defining another call method
// for the same class
String call(String a) => 'Welcome to $a!';
}
// Main Function
void main() {
// Creating instance of class
var geek_input = GeeksForGeeks();
// Calling the class through its instance
var geek_output = geek_input('Geeks', 'For', 'Geeks');
// Printing the output
print(geek_output);
}
Output:
compileDDC
main.dart:7:10: Error: 'call' is already declared in this scope.
String call(String a) => 'Welcome to $a!';
^^^^
main.dart:4:10: Context: Previous declaration of 'call'.
String call(String a, String b, String c) => 'Welcome to $a$b$c!';
^^^^
- A callable class in Dart is a class that can be invoked like a function. To create a callable class, you must define a call method inside the class. The call method can take any number of arguments and return any type of value.
Dart
// Defines a class named 'Adder' that implements the call method
class Adder {
// The call method allows an instance of
// this class to be called like a function
int call(int a, int b) {
// Returns the sum of the two integers
return a + b;
}
}
void main() {
// Creates an instance of the Adder class
var adder = Adder();
// Calls the instance directly as if it were a function
var sum = adder(1, 2);
// Prints the result (3) to the console
print(sum);
}
- You can also define a call method inside an anonymous function, which allows you to create a callable function on the fly. Here is an example:
Dart
void main() {
// Defines an anonymous function (lambda)
// and assigns it to the variable 'adder'
var adder = (int a, int b) {
// Returns the sum of a and b
return a + b;
};
// Calls the anonymous function and
// stores the result in 'sum'
var sum = adder(1, 2);
// Prints the result (3) to the console
print(sum);
// prints 3
}
Example:
Dart
void main() {
// Creates an instance of the Adder class
var adder = Adder();
// Calls the instance directly using the call method
var sum = adder(1, 2);
// Prints the result (3) to the console
print(sum);
// prints 3
}
// Defines a class named 'Adder'
class Adder {
// A regular method that adds two integers
int add(int a, int b) {
return a + b;
}
// The call method allows the instance
// to be called like a function
int call(int a, int b) {
// Calls the add method internally
return add(a, b);
}
}
Output:
3
Conclusion
- Dart allows the creation of callable classes through the use of the call() method.
- A callable class allows an instance to be used and invoked like a function.
- Each class can have only one call() method (method overloading is not permitted).
- Callable functions can also be created using anonymous functions, although these do not qualify as callable classes.
Explore
Dart Tutorial
7 min read
Basics
Data Types
Control Flow
Key Functions
Object-Oriented Programming
Dart Utilities
Dart Programs
Advance Concepts