IAmYourFaja
IAmYourFaja

Reputation: 56894

Dart enum compiler error

I am trying to define the following "enum" in Dart:

class Place {
    static const SigninPlace = const Place._("", new Map<String,String> { "fizz": "buzz"});
    static const SignoutPlace = const Place._("", null);

    static get values => [SigninPlace, SignoutPlace];

    final String name;
    final Map<String,String> params;

    const Place._(this.name, this.params);
}

But am getting a compiler error on the SigninPlace declaration:

Arguments of a constant creation must be constant expressions

Why, and what can I do to fix this? (Hint: each Place's map of <String,String> won't change and is known at startup).

Upvotes: 2

Views: 213

Answers (1)

Alexandre Ardhuin
Alexandre Ardhuin

Reputation: 76183

When you define const all its member have to be constant expression. In your case new Map<String,String> { "fizz": "buzz"} is not a constant expression. You have to used const <String,String>{ "fizz": "buzz"} to create a constant Map.

class Place {
  static const SigninPlace = const Place._("", 
      const <String,String>{ "fizz": "buzz"});
  static const SignoutPlace = const Place._("", null);

  static get values => [SigninPlace, SignoutPlace];

  final String name;
  final Map<String,String> params;

  const Place._(this.name, this.params);
}

Upvotes: 2

Related Questions