Flutter - How to instantiate an object from a Type

11,432

Solution 1

You cannot create an instance of a class from a Type object representing the class, at least not without mirrors (and neither the Flutter nor the web platform supports mirrors).

You cannot tear-off a constructor (yet, I hope we'll add that feature at some point), so the option of creating a static helper function which calls the constructor, and passing the helper function around, is the best current solution.

If you know the number of arguments required by such a function, you don't need to use Function.apply, you can just call it directly.

Solution 2

You can tear-off a function if you don't add the parentheses (). You don't need dart:mirror.

void main() {
  // Tear off a global function
  Function myFunction = MyClass.someStaticFunction;

  // Call it statically
  myFunction('arg');

  // Call it dynamically
  Function.apply(myFunction, ['arg']);

  // Tear it off with a precise Function Type
  void Function(String) myFunction2 = MyClass.someStaticFunction;
}

class MyClass {
  static void someStaticFunction(String someArg) {
    print('someStaticFunction $someArg');
  }
}

Solution 3

If you don't want to user reflection, you can do this.

class A {
}
class B {
}

dynamic init(Type cls){
  switch (cls) {
    case A:
      return A();
    case B:
      return B();
  }
}

void main() {
  print(init(A));
  print(init(B));
}

As a best practice, to keep organized and avoid using dynamic, you could extend from a common base and call a static constructor.

class A extends Base{
}
class B extends Base{
}

abstract class Base{
  static Base init(Type cls){
    switch (cls){
      case A:
        return A();
      case B:
        return B();
  }}
}

void main() {
  print(Base.init(A));
  print(Base.init(B));
}

Output:

Instance of 'A'
Instance of 'B'

Process finished with exit code 0
Share:
11,432

Related videos on Youtube

stackunderflow
Author by

stackunderflow

Updated on September 14, 2022

Comments

  • stackunderflow
    stackunderflow over 1 year

    My aplogoze for the original question. I'm getting confused. This is my actual question.

    I have a situation where I need to use some defined classes as an object of type Type and instantiate it's object instance. As for now, I'm using self defined static instantiator function to call the constructor and it's working good. So I can just call Function.apply() to create an object. Rather than doing that, is there any way I can use the class's Type to emulate the class's constructor.


    What have I tried? They say to use Mirror, a solution for Dart's reflection.

    import 'dart:mirrors' have failed with error

    Target of URI doesn't exists

    Or at least if it's not ready provided I don't know where to get the package.

    import 'package:reflectable/mirrors.dart' with package from git://github.com/dart-lang/reflectable.git only defines all Mirror implemented classes, but function reflect is missing.

    • Günter Zöchbauer
      Günter Zöchbauer over 5 years
      Can you provide a concrete example of what you try to accomplish and at what step you're stuck?
    • Günter Zöchbauer
      Günter Zöchbauer over 5 years
      Mirrors is not available in Flutter.
  • stackunderflow
    stackunderflow over 5 years
    i'm sorry sir. original question actually my mistake. i've updated it. +1 for your answer
  • stackunderflow
    stackunderflow over 5 years
    i'm sorry sir. original question actually my mistake. i've updated it. +1 for your answer
  • lrn
    lrn over 5 years
    Makes sense, I've updated the answer (rewritten, more like).
  • Eric Platon
    Eric Platon about 3 years
    Any risk of circularity in this alternative? When sub-classes are declared in a different file, the two files need to import each other.