我正在尝试获取(通过获取我的意思是创建一个新的)一个类的实例。这就是我所做的:
file_one.dart
import 'dart:isolate';
void main() {
SendPort sendPort = spawnUri('file_two.dart');
sendPort.call('get new instance')
.then(print);
}
file_two.dart
import 'dart:isolate';
import 'dart:mirrors';
class TestClass {}
void main() {
port.receive((message, SendPort replyTo) {
MirrorSystem current = currentMirrorSystem();
LibraryMirror isolate = current.isolate.rootLibrary;
Map classes = isolate.classes;
ClassMirror test = classes[const Symbol('TestClass')];
TestClass testClass = test.newInstance(const Symbol(''), []).reflectee;
replyTo.send(testClass);
});
}
它什么也不打印,但如果我发送一个字符串,例如'SomeString',它会打印'SomeString'。任何解决方法?