Dart ClassMirror newInstance method says no such constructor

Issue

I tried to dynamically create a new instance of a class like this:

this.componentClass.newInstance(new Symbol(''), [this, el]).reflectee;

The class reflected in this.componentClass is called ButtonComponent and it is a subclass of Component. When running a test on this, I get an error:

Test failed: Caught No constructor 'ButtonComponent.' declared in class 'ButtonComponent'.
NoSuchMethodError : method not found: 'ButtonComponent.'
Receiver: Type: class 'ButtonComponent' Arguments: [...]

There are default constructors in both Component and ButtonComponent classes. Here is the code, to make sure I didn’t miss anything:

class Component {

  Element  element ;
  Template template;

  Component(this.template, this.element) {
    this.element.replaceWith(new Element.html(template.html));
  }

}

class ButtonComponent extends Component {

  ButtonComponent(template, element) : super(template, element) {};

}

Any ideas what is wrong here? Thank you.

Solution

I just made a similar test in 1.0.0.3_r30187 and I don’t get this error. If you don’t use the last stable version of Dart you should update your version.

Here’s my tested code :

import 'dart:html';
import 'dart:mirrors';

class Component {
  Element  element ;

  Component(this.element) {
    this.element.children.add(new Element.html("<b>Dart rocks</b>"));
  }
}

class ButtonComponent extends Component {
  ButtonComponent(element) : super(element);
}

main() {
  final a = reflectClass(ButtonComponent).newInstance(new Symbol(''), 
      [document.documentElement]).reflectee;
  print(a); // display : Instance of 'ButtonComponent'
}

Answered By – Alexandre Ardhuin

Answer Checked By – Jay B. (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *