Table of contents
1.
Introduction
2.
Implementation
3.
Frequently Asked Questions
3.1.
Is call() method support the function overloading mechanism?
3.2.
Are we allowed to pass a function as an argument?
3.3.
Which type of variable takes a function as its value?
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

Callable Classes in Dart

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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.

Frequently Asked Questions

Is call() method support the function overloading mechanism?

No, the call() function of the callable class does not support the function overloading mechanism. Since the callable class does not allow more than one call method, therefore we cannot implement function overloading. 

 

Are we allowed to pass a function as an argument?

The anonymous function gives us the freedom to pass a function as an argument. After we pass a function as a parameter, it is unnamed that's why it is an anonymous function.  

 

Which type of variable takes a function as its value?

After we pass a function as an argument, then the variable which takes that anonymous function is of type Function.

Conclusion

In this article, we've extensively discussed the Callable class in dart and its implementation. We have discussed how to pass parameters and take the return value using appropriate examples.

 We hope that this blog has helped you enhance your knowledge regarding the Callable class of Dart. Do check out the awesome content on the Coding Ninjas Website, Android DevelopmentCoding Ninjas Studio ProblemsCoding Ninjas Studio Interview BundleCoding Ninjas Studio Interview ExperiencesCoding Ninjas CoursesCoding Ninjas Studio Contests, and Coding Ninjas Studio Test SeriesDo upvote our blog to help other ninjas grow.

Happy Coding!

Live masterclass