mercredi 15 juin 2022

Dart/Reflectable: How to get all subclasses from an abstract base class

I want to use the Dart/reflectable framework to find all subclasses of a specific base class and I struggle with that use case.

I have

  1. An abstract base class with a few getters:
abstract class MyBaseClass {
    String get name;
    List<MyValueType> get values;
}
  1. Several classes that implement MyBaseClass:
class A implements MyBaseClass {
   @override
   String name = 'AClass';
   
   @override
   List<MyValueType> = [MyValueType.X, MyValueType.Y]; 
}

class B implements MyBaseClass {
   @override
   String name = 'BClass';
   
   @override
   List<MyValueType> = []; 
}

My goal is to fetch all classes that implement MyBaseClass and read their properties.

So, I created:

class Reflector extends Reflectable {
  const Reflector()
      : super(invokingCapability); 
}

const reflector = const Reflector();
  1. How do I fetch a list of classes? I only found the InstanceMirror.reflect() which only delivers one result, not many.
  2. It is not clear, how the annotation must be set. When trying to fetch all MyBaseClass implementations, do I need to annotate only my abstract MyBaseClass or do I need to annotate classes A and B or do I need to annotate all three classes?
  3. Which capabilities do I need? In my test case I got this exception: NoSuchCapabilityError: no capability to invoke the getter "name" but was not able to solve this.

Thanks in advance, any help is appreciated!





Aucun commentaire:

Enregistrer un commentaire