在dart中输入一个js地图

时间:2017-10-20 07:19:11

标签: javascript dart

如何在dart中移植此const?

const CHANNEL_STATES = {
  closed: "closed",
  errored: "errored",
  joined: "joined",
  joining: "joining",
  leaving: "leaving",
}

我需要它来打电话给前。 CHANNEL_STATES.closed 感谢

2 个答案:

答案 0 :(得分:2)

您可以使用类和常量静态成员执行此操作:

class CHANNEL_STATES {
  static const String closed = "closed";
  static const String errored = "errored";
  static const String joined = "joined";
  static const String joining = "joining";
  static const String leaving = "leaving";
}

答案 1 :(得分:0)

我不确定你要求的是什么,但Dart中类似的地图常数将是:

const channelStates = const {
  "closed": "closed",
  "errored": "errored",
  "joined": "joined",
  "joining": "joining",
  "leaving": "leaving",
};

Dart map文字可以使用任何对象作为键,因此要使用字符串作为键,您需要引用它。您不能像JavaScript一样使用不带引号的标识符。