简体   繁体   中英

Port a js map in dart

How can i port this const in dart?

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

I need it to call ex. CHANNEL_STATES.closed Thanks

You can do that with a class and constant static members:

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";
}

I'm not sure exactly what you are asking for, but a similar map constant in Dart would be:

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

A Dart map literal can use any object as key, so to use a string as key, you need to quote it. You can't use just an unquoted identifier like in JavaScript.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM