Search code examples
templatesconstructordartdart-polymer

Dart polymer 'does not have default constructor'


I have several templates (Polymer and Dart) working fine. But I cannot understand the Dart editor complaining in the case where I have a (my) template within another of (my) templates. I have a template for "staff" - it extends PolymerElement.
I have a template for "person" - it extends PolymerElement.
I cannot dynamically create a "person" in "staff", with the complaint: person 'does not have default constructor'.
Here is my code for staff:

import 'package:polymer/polymer.dart';
import 't_person.dart';
@CustomTag('t-staff')
class tstaff extends PolymerElement {
   tstaff.created() : super.created() {}
void addPersonButton(){
   tPerson tpe = new tPerson(); //ERROR HERE 'tPerson does not have default constructor'
    ....

Here is my code for tPerson (no errors):

import 'package:polymer/polymer.dart';
@CustomTag('t-person')
class tPerson extends PolymerElement {
  tPerson.created() : super.created() {   }
 }

Am I not allowed to use templates in this way? Is there a limit on the depth of children templates?


Solution

  • If you want to create a new instance of a Polymer element you need to do it like new Element.tag('t-person');.
    If you add this to the default constructor you get a more convenient way.

    @CustomTag('t-person')
    class tPerson extends PolymerElement {
    
      tPerson.created() : super.created();
    
      factory tPerson tPerson() {
        return new Element.tag('t-person');
      }
    }