In this tutorial you will learn about the Dart Callable classes and its application with practical example.
Dart Callable Classes
In Dart, the callable class allows its instances to be called like a function. To make a class callable class you are required to implement a call() method in it. Lets understand it by example –
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class Employee{ String call(String name, int age){ return "Employee name is $name and Age Is $age"; } } void main(){ Employee emp = new Employee(); var msg = emp("John", 30); print("W3Adda - Dart Callable Class Example"); print(msg); } |
In the above example, the Employee class defines a call() function that takes two parameters String name and Integer age and return a message with this information.We have created an object emp of Employee class and called it like this –
1 |
var msg = emp("John", 30); |