I'm trying to call a method of an object in dart, that is mirrored using dart:mirrors. The object I'd like to use is a instance of a class, that is defined in a library of another file.
main.dart
import 'dart:mirrors';
main(List<String> args) {
var libName = args[0];
var className = args[1];
var methodName = args[2];
MirrorSystem mirrors = currentMirrorSystem();
LibraryMirror lm = mirrors.findLibrary(new Symbol(libName));
ClassMirror cm = lm.declarations[new Symbol(className)];
InstanceMirror im = cm.newInstance(new Symbol(''), []);
im.invoke(new Symbol(methodName), []);
}
I wrote another file for testing:
test.dart
library test;
class TestClass {
doStuff() => print("doStuff was called!");
}
If I run main.dart I get an exception:
dart main.dart test TestClass doStuff
Unhandled exception:
Exception: There is no library named 'test'
...
I can add an import statement of the library in main.dart and everything is fine
import 'test.dart';
but I know the name of the library only at runtime.
How can I import the library at runtime or is there a different approach?
DeferredLibrarywon't help? - JAredeferred loadingyou can split libraries from the main package and to be loaded on demand. But you still have to import the library.deferred loadingalso works in other scenarios (plain Dart) but I thought using this scenario would demonstrate the point more clearly. - Günter Zöchbauer