In dart how to let an isolate create instance of child class Object from different package?

174 views Asked by At

I have a isolate which needs ClassParent kind of object. This isolate resides in PackageA. I have another PackageB, which implements this in ClassChild.

Now I want my isolate to pick up instances of ClassParent kind at runtime, that too such instances resides in a different package.

PackageA

//packageA
class ClassParent{
 sayHello(){
   print("Hello");
 }
}

functionToRunInIsolate(SendPort sp){
  ClassParent obj1 = createAndGiveRunTimeImplementation();
  obj1.sayHello();
}

PackageB

class ClassChild extends ClassParent{
 sayHello(){
   print("Halo");
 }
}

main() async{
ReceivePort rp = ReceivePort();

  rp.listen((message) {
    print(message);
  });

Isolate i = await Isolate.spawn(functionToRunInIsolate, rp.sendPort);
}

Its must be noted that the implementation is in a completely different package. functionToRunInIsolate should use Implementation from PackageB.

PackageA has no idea that which package will provide the implementation. Its should pick up the instance base don configs passed into it.

0

There are 0 answers