PolymerElement构造函数

时间:2015-01-03 14:14:16

标签: dart dart-polymer

我试图更改标准构造函数以添加myConstructor

基于代码here,我编写了以下语句:

@CustomTag('main-app')
class MainApp extends PolymerElement {

  factory MainApp.custom() {
    MainApp = new Element.tag('main-app');
    myConstructor();
  }
MainApp.created() : super.created();
void myConstructor() {
    print("myConstructor Started");
  }

不幸的是,编辑显示了一些问题:
声明MainApp = new Element.tag('main-app')显示错误:
A Value of Type 'Element' cannot be assigned to a variable of type 'Type'.

Statement myConstructor()显示错误:"无法从工厂构造函数访问实例成员"

任何人都有建议如何解决错误?

修改: 目前的代码如下所示:

@CustomTag('main-app')
class MainApp extends PolymerElement {

  @observable int counter = 0;


      factory MainApp.custom() {
    MainApp mainApp = new Element.tag('main-app');
    mainApp.myConstructor();
    return mainApp;
  }

  /// Constructor used to create instance of MainApp.
  MainApp.created() : super.created();

MainApp ma = new MainApp.custom();

  void myConstructor() {
    print("myConstructor Started");
    counter = 1;
  }

出现以下错误:

Exception: type 'HtmlElement' is not a subtype of type 'MainApp' of 'mainApp'.
  MainApp.MainApp.custom    
  MainApp.MainApp.created

编辑版本2:

@CustomTag('main-app')
class MainApp extends PolymerElement {

  Controller controller = new Controller();

  @observable int counter = myConstructor();
  @observable int totalGlypto = 0;

  /// Constructor used to create instance of MainApp.
  MainApp.created() : super.created();


    static int myConstructor() {
    print("myConstructor Started");
    return controller.getTotal();
  }

  void setupCheckIn(Event e, var detail, Node target) {
    print("Setup started");
    controller.checkInGlypto();
    counter = controller.getTotal();
  }

  void resetCheckIn(Event e, var detail, Node target) {
    print("reset started");
  }

  void loadCheckIn(Event e, var detail, Node target) {
    print("load started");
    controller.loadData();
    counter = controller.getTotal();
  }


}

可以下载代码here

2 个答案:

答案 0 :(得分:0)

您正在使用类MainApp作为变量。更改以下行

MainApp = new Element.tag('main-app');

MainApp mainApp = new Element.tag('main-app');

var mainApp = new Element.tag('main-app');

答案 1 :(得分:0)

好吧,现在我觉得我找到了答案,关键是以下代码:

@observable int get counter => myConstructor();
相关问题