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.
Example 1: Implementing a callable class in Dart.
Dart
class GeeksForGeeks {
String call(String a, String b, String c) => 'Welcome to $a$b$c!' ;
}
void main() {
var geek_input = GeeksForGeeks();
var geek_output = geek_input( 'Geeks' , 'For' , 'Geeks' );
print(geek_output);
}
|
Output:
Welcome to GeeksForGeeks!
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.
Example 2: Implementing multiple callable functions in a class of Dart.
Dart
class GeeksForGeeks {
String call(String a, String b, String c) => 'Welcome to $a$b$c!' ;
String call(String a) => 'Welcome to $a!' ;
}
void main() {
var geek_input = GeeksForGeeks();
var geek_output = geek_input( 'Geeks' , 'For' , 'Geeks' );
print(geek_output);
}
|
Output:
Error compiling to JavaScript:
main.dart:3:10:
Error: 'call' is already declared in this scope.
String call(String a) => 'Welcome to $a!';
^^^^
main.dart:2:10:
Info: Previous declaration of 'call'.
String call(String a, String b, String c) => 'Welcome to $a$b$c!';
^^^^
main.dart:8:31:
Error: Can't use 'call' because it is declared more than once.
var geek_output = geek_input('Geeks', 'For', 'Geeks');
^^^^
main.dart:8:31:
Error: The method 'call' isn't defined for the class 'GeeksForGeeks'.
- 'GeeksForGeeks' is from 'main.dart'.
var geek_output = geek_input('Geeks', 'For', 'Geeks');
^
Error: Compilation failed.
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
class Adder {
int add( int a, int b) {
return a + b;
}
}
void main() {
var adder = Adder();
var sum = adder(1, 2);
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() {
var adder = ( int a, int b) {
return a + b;
};
var sum = adder(1, 2);
print(sum);
}
|
Example:
Dart
void main() {
var adder = Adder();
var sum = adder(1, 2);
print(sum);
}
class Adder {
int add( int a, int b) {
return a + b;
}
int call( int a, int b) {
return add(a, b);
}
}
|
Output: 3
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
28 Dec, 2022
Like Article
Save Article