Home > Net >  How to dynamically instantiate a class (Function.apply for class)?
How to dynamically instantiate a class (Function.apply for class)?

Time:03-25

In Dart, I can dynamically call a function using Function.apply:

Function.apply(foo, [1,2,3], {#f: 4, #g: 5});

gives exactly the same result as

foo(1, 2, 3, f: 4, g: 5).

Question: Does a similar thing exist for instantiating classes?

Expected result would look something like:

class foo {
  final String boo;
  int? moo;
  foo({required this.boo, this.moo})
}

...

var params = {boo: 'A string value', moo: 121};
Class.apply(foo, params);

// Gives the result:
foo(boo: 'A string value', moo: 121);

CodePudding user response:

Function.apply isn't type-safe, so you should avoid using it if you can.

If you really want to use it with a constructor, you can use it with constructor tear-offs (added in Dart 2.15), which are just Functions:

class Foo {
  final String boo;
  int? moo;

  Foo({required this.boo, this.moo});
  
  @override
  String toString() => 'Foo(boo: "$boo", moo: $moo)';
}

void main() {
  var params = {#boo: 'A string value', #moo: 121};
  var result = Function.apply(Foo.new, [], params);
  print(result); // Prints: Foo(boo: "A string value", moo: 121)
}

CodePudding user response:

As far as I know, you can make use of static methods if you want to create an instance without using another instance. Here is a sample:

class Foo {
  final String boo;
  final int moo;
  Foo({this.boo, this.moo});
  static fromValues({String boo, int moo}) {
    return Foo(boo: boo, moo: moo);
  }
}

void main() {
  var params = {#boo: 'A string value', #moo: 121};
  var fooObject = Function.apply(Foo.fromValues, [], params);
  print(fooObject);
  print(fooObject.boo);
  print(fooObject.moo);
}

Another way is to add 'call' function to class to make it's objects callable and use an object of the class to create new objects. Here is a sample:

class Foo {
  final String boo;
  final int moo;

  Foo({this.boo, this.moo});

  call({String boo, int moo}) {
    return Foo(boo: boo, moo: moo);
  }
}

void main() {
  Foo aFoo = Foo(boo: 'nothing', moo: 0);
  var params = {#boo: 'A string value', #moo: 121};
  var fooObject = Function.apply(aFoo, [], params);
  print(fooObject);
  print(fooObject.boo);
  print(fooObject.moo);
}
  •  Tags:  
  • dart
  • Related