I just want have a final List with colors :
final List<String> COLORS = ['#cefbe4', '#81ec72', '#5cd646'];
final num MAX = 90;
class Square {
// ...
It’s ok for MAX but it does not compile for list :
Initializer must be a compile time constant
I also tried :
static final num MAX = 90;
class Square {
final List<String> COLORS = ['#cefbe4', '#81ec72', '#5cd646'];
New error : “initializer must be a compile time constant”
The only solution, I found is to initialise in constructor…
I don’t like this solution : I don’t want a list instance by Square object.
How can I do ?
I don’t get the same errors as you. In the first case I get a compile time warning “Expected constant expression” and in the second case another compile-time warning and a runtime error. Try the latest SDK which you can find here.
In this case static it doesn’t make any sense because static denotes class variables which are the same for all instantiated objects of the same class. And in this case MAX is outside a class boundary. Hence a runtime error is shown and a compile time warning (“Top-level field cannot be static”).
I guess both MAX and COLORS are supposed to be the same for all instances of the Square class. So it makes sense to do this: