Introduction
In this article, we will be learning about the Callable classes in dart. Dart is an object-oriented programming language. It supports all the features of OOPs and even further you can use the concept of callable classes. Using the callable class, you can make your instance acts as a function means now the instance of your callable class will be a function you need to pass the parameters and accept the return value. To implement this functionality, we have to make a call() method in the callable class.
Syntax:
class Name
{
//Code for other content.
return_type call(data_type Parameters)
{
//Content of call() function.
}
}
Here you can notice that we have defined a call method along with its parameters and return value.
Implementation
A Callable class allows a function to be called on its instance. This feature of the Dart language comes in handy when creating named functions.
Example: In this example, we show the simple use case of the callable function.
Code:
class Demo
{
//Our call() method.
String call(String name)
{
//Function returning a value.
return "Hi $name, welcome to Coding Ninjas!";
}
}
void main()
{
//Creating a Callable class's object.
var obj = Demo();
//Calling the call() method and storing return value.
var str = obj("Ethan Hunt");
//Printing the returned string.
print(str);
}
Output:
Hi Ethan Hunt, welcome to Coding Ninjas!
Explanation: Here “Demo” is our callable class whose call function is returning a string.
Example: This example shows multiple callable classes.
Code:
class User
{
//Our call() method.
String call(String id, String name)
{
//Functiom returning a value.
return "Id: $id, Name: $name";
}
}
class Courses
{
//Our call() method.
String call(String c1)
{
//Functiom returning a value.
return c1;
}
}
void main()
{
//Creating a Callable class's object.
var user = User();
var course = Courses();
//Calling the function and storing return value.
var intro = user("112233", "Ninja1");
var cou = course("Android Dev.");
//Printing id, name and courses enrolled in.
print(intro);
print("Enrolled in: $cou");
}
Output:
Id: 112233, Name: Ninja1
Enrolled in: Android Dev.
Example: This example shows that you can only construct one call() method in a callable class.
Code:
//Given example uses two call() methods in one callable class.
class Demo3
{
//Our call() methods.
String call(String name)
{
//Functiom returning a value.
return "Hi $name you are in the 1st call() method.";
}
String call(String name, int age)
{
//Functiom returning a value.
return "Hi $name of age $age you are in the 2nd call() method.";
}
}
void main()
{
//Creating a Callable class's object.
var obj = Demo3();
//Calling the function and storing return value.
var str1 = obj("Ninja1");
var str2 = obj("Ninja1", 21);
//Printing id, name and courses enrolled in.
print(str1);
print(str2);
}
Output:
Error compiling to JavaScript:
Info: Compiling with sound null safety
Warning: Interpreting this as package URI, 'package:dartpad_sample/main.dart'.
lib/main.dart:10:10:
Error: 'call' is already declared in this scope.
String call(String name, int age)
^^^^
lib/main.dart:5:10:
Info: Previous declaration of 'call'.
String call(String name)
^^^^
lib/main.dart:24:17:
Error: Can't use 'call' because it is declared more than once.
var str1 = obj("Ninja1");
^^^^
lib/main.dart:24:17:
Error: The method 'call' isn't defined for the class 'Demo3'.
- 'Demo3' is from 'package:dartpad_sample/main.dart' ('lib/main.dart').
var str1 = obj("Ninja1");
^
lib/main.dart:25:17:
Error: Can't use 'call' because it is declared more than once.
var str2 = obj("Ninja1", 21);
^^^^
lib/main.dart:25:17:
Error: The method 'call' isn't defined for the class 'Demo3'.
- 'Demo3' is from 'package:dartpad_sample/main.dart' ('lib/main.dart').
var str2 = obj("Ninja1", 21);
^
Error: Compilation failed.
Explanation: The error shows that dart does not support more than one call() method, which means you are not allowed to make two call() methods in one callable class.





