Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter - How to instantiate an object from a Type

Tags:

flutter

dart

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.

like image 206
stack underflow Avatar asked Dec 16 '18 11:12

stack underflow


3 Answers

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');
  }
}
like image 95
Xavier Avatar answered Sep 20 '22 15:09

Xavier


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.

like image 30
lrn Avatar answered Sep 21 '22 15:09

lrn


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
like image 40
Salvaterra Avatar answered Sep 20 '22 15:09

Salvaterra