Dart - Pass a Class/Type as a Function Parameter

This tutorial shows you how to pass a class as a parameter of a function in Dart.

A function in Dart can accept various parameter types. While it's not common to pass a class or a type as a parameter, I am going to show you how to do it.

Pass a Type as a Parameter

There is a case where we want to have a function whose one of the parameters is a class or a type. Inside the function, we want to do several things with the class, such as creating an instance or calling the method of a class. While it's possible to pass the name as a string, there is a more proper way to do it.

Dart has an abstract class named Type, which holds the Runtime representation of a type. A type can be a class, enum, or function. Instead of passing the string name, it's recommended to pass the Type object. It can be obtained from the type literal (type name) or the runtime type of an object.

  Type type1 = int;
  Type type2 = myObject.runtimeType;

If you only want to check the passed type object, you can do it by comparing it using an equal operator. But if you need to do something more, you may need to use Dart's reflection API. Dart has a type named ClassMirror. It reflects a class in Dart language. To use it, you need to add the following import.

  import 'dart:mirrors'

After adding the import, you can call a function called reflectClass, which returns a ClassMirror object.

  ClassMirror reflectClass(Type key);

The ClassMirror is the object that you can pass as an argument. With that object, you can do several things which include creating new instances, calling a method, or getting the class members.

For example, there is a class named Foo.

  class Foo {
    String getMessage(String value) {
      return 'Foo $value';
    }
  }

The ClassMirror object of the Foo class can be obtained by using the code below.

  ClassMirror fooClassMirror = reflectClass(Foo);

Below is an example of a function that accepts a Type parameter. First, we get the ClassMirror of the type. Then, we can do a specific logic based on the passed type.

Checking whether the passed type is a specific type can be done by comparing it using an equal operator. For your information, it's also possible to get the type from a ClassMirror object by accessing the reflectedType property.

In the example below, if the type is Foo, we create an instance of the class by getting the InstanceMirror first. Then, get the actual instance from the reflectee property. After getting the instance, you can access all public members of the class.

  void doSomething(Type type) {
    ClassMirror classMirror = reflectClass(type);
  
    if (type == Foo) {
      InstanceMirror im = classMirror.newInstance(Symbol(''), []);
      Foo instance = im.reflectee;
  
      String message = instance.getMessage('woolha.com');
      print('The message is $message');
    }
  }

Summary

To pass a class or a type as a function parameter, it's recommended to pass it as a Type object. It can be obtained by directly passing the type literal or from the runtimeType property of an object. Then, you can get the ClassMirror object of the type by using the reflectClass function which requires you to pass the Type as the argument.

You can also read about: