Search code examples
dartdart-polymer

Reference static consts from HTML


I'd like to use a const within a Dart class and reference its value in my HTML. For example:

Dart class:

Class MyClass 
{
        static const String MY_VALUE = "foo";
}

HTML:

<input value="{{MyClass.MY_VALUE}}">

Can we do this?


Solution

  • No, as far as I know, you cannot use a static const in your template. The template expects instance methods and getters. There is however an easy workaround: define a getter that returns the value of the const, and then use that getter in your HTML.

    Here is the code for the getter:

    String get myValue => MY_VALUE;
    

    And here is the use of the getter in the HTML:

    <input value="{{myValue}}">