Concept of Callable Classes in Dart

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



filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
}

chevron_right


 

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
}

chevron_right


 

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.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.