10

In dart is it possible to instantiate a class from a string?

For example:

  • vanilla in javascript:
var myObject = window[classNameString];
  • Objective-C:
id myclass = [[NSClassFromString(@"MyClass") alloc] init];
Günter Zöchbauer
  • 490,478
  • 163
  • 1,733
  • 1,404
1dayitwillmake
  • 2,110
  • 3
  • 22
  • 34

4 Answers4

4

You need to know the library name and class name to make things work properly. Assume you know both, the below example will instantiate the TestClass and call doStuff on it.

library test;

import "dart:mirrors";

class TestClass {
  doStuff() => print("doStuff was called!");
}

main() {
  MirrorSystem mirrors = currentMirrorSystem();
  LibraryMirror lm = mirrors.libraries['test'];
  ClassMirror cm = lm.classes['TestClass'];
  Future tcFuture = cm.newInstance('', []);
  tcFuture.then((InstanceMirror im) {
    var tc = im.reflectee;
    tc.doStuff();
  });
}

A few notes about this solution:

  1. The library test we are trying to load the class from is already imported in the VM, which makes this case a bit easier.
  2. the call the newInstance allows for passing parameters to the constructor. Positional arguments are implemented, but named parameters are not yet implemented (as of the M2 release).
  3. newInstance returns a Future to allow it to work across isolates.
Kyrra
  • 478
  • 3
  • 3
  • `newInstance` doesn't return a `Future` because instantiating the class may require an async operation, that's not true, everything is already loaded. It returns a `Future` because the Mirrors API is intentionally asynchronous, as it will work across isolates in the future (and cross-isolate communication is always asynchronous). If you carefully inspect the `newInstance` invocation (and others like `get/setField` or `invoke` too), you will note that under the hood, it works synchronously. That might obviously change in the future. – Ladicek Jan 10 '13 at 09:37
  • Note: this might not work when compiled to JavaScript. The dart2js compiler doesn't yet fully support mirrors. – Seth Ladd Jan 10 '13 at 21:46
  • uh .. this makes Dart look less dynamic than Java .. reflection is a very important feature .. – R.Moeller Dec 21 '13 at 16:03
4

The syntax has changed. I got it working this way

library test;

import "dart:mirrors";

class TestClass {
  doStuff() => print("doStuff was called!");
}

main() {
  MirrorSystem mirrors = currentMirrorSystem();
  LibraryMirror lm = mirrors.libraries.values.firstWhere(
      (LibraryMirror lm) => lm.qualifiedName == new Symbol('test'));

  ClassMirror cm = lm.declarations[new Symbol('TestClass')];

  InstanceMirror im = cm.newInstance(new Symbol(''), []);
  var tc = im.reflectee;
  tc.doStuff();
}

If there are more libraries named 'test' this will fail though.

Günter Zöchbauer
  • 490,478
  • 163
  • 1,733
  • 1,404
1

Try:

Map models = {"Player": Player.instatiate};
var player = models["Player"]();

class Player{
  static instatiate() => Player();
}
0

This was an issue that has plagued me until I figured that I could implement a crude from method to handle the conversion of encoded Json Objects/strings or Dart Maps to the desired class.

Below is a simple example that also handles nulls and accepts JSON (as the string parameter)

import 'dart:convert';

class PaymentDetail
{
  String AccountNumber;
  double Amount;
  int ChargeTypeID;
  String CustomerNames;

  PaymentDetail({
    this.AccountNumber,
    this.Amount,
    this.ChargeTypeID,
    this.CustomerNames
  });

  PaymentDetail from ({ string : String, object : Map  }) {
     var map   = (object==null) ? (string==null) ? Map() : json.decode(string) : (object==null) ? Map() : object;
     return new PaymentDetail(
        AccountNumber             : map["AccountNumber"] as String,
        Amount                    : map["Amount"] as double,
        ChargeTypeID              : map["ChargeTypeID"] as int,
        CustomerNames             : map["CustomerNames"] as String
     );
  }

}

Below is it's implementation

 PaymentDetail payDetail =  new PaymentDetail().from(object: new Map());

 PaymentDetail otherPayDetail =  new PaymentDetail().from(object: {"AccountNumber": "1234", "Amount": 567.2980908});

Once again, this is simplistic and tedious to clone throughout the project but it works for simple cases.

Ian Mbae
  • 119
  • 2
  • 11