map-keys-ordering

Group
Style
Path
lint/style/map-keys-ordering
Default
Off, opt in
Origin
Falcon
Rule
258 of 512

Flags literal string map keys that violate the configured order.

Set order to "ascending" or "descending" for case-sensitive lexicographic ordering. Without a valid explicit order, this rule is inactive. Dynamic keys, spreads, collection control-flow, interpolations, adjacent strings, and escaped non-raw strings break the comparison sequence.

Invalid

example.dartdart
final first = {'zebra': 1, 'apple': 2};
final second = {"red": 1, "blue": 2};
final raw = {r'zebra': 1, r'apple': 2};
final mixedQuotes = {'zebra': 1, "apple": 2};
final nested = {'outer': {'zebra': 1, 'apple': 2}};
Reorder literal string map keys to follow the configured order.
1final first = {'zebra': 1, 'apple': 2};
Reorder literal string map keys to follow the configured order.
1final first = {'zebra': 1, 'apple': 2};
2final second = {"red": 1, "blue": 2};
Reorder literal string map keys to follow the configured order.
2final second = {"red": 1, "blue": 2};
3final raw = {r'zebra': 1, r'apple': 2};
Reorder literal string map keys to follow the configured order.
3final raw = {r'zebra': 1, r'apple': 2};
4final mixedQuotes = {'zebra': 1, "apple": 2};
Reorder literal string map keys to follow the configured order.
4final mixedQuotes = {'zebra': 1, "apple": 2};
5final nested = {'outer': {'zebra': 1, 'apple': 2}};

Valid

example.dartdart
final sorted = {'apple': 1, 'zebra': 2};
final singleton = {'zebra': 1};
final numbers = {2: 1, 1: 2};
Map<String, int> withSpread(Map<String, int> other) => {'zebra': 1, ...other, 'apple': 2};
Map<String, int> dynamicKey(String key) => {'zebra': 1, key: 2, 'apple': 3};
Map<String, int> interpolation(String key) => {'zebra': 1, '$key': 2, 'apple': 3};
final escaped = {'zebra': 1, '\u0061pple': 2};
Map<String, int> conditional(bool flag) => {'zebra': 1, if (flag) 'middle': 2, 'apple': 3};
final duplicates = {'apple': 1, 'apple': 2};
final adjacent = {'zebra': 1, 'a' 'pple': 2};

Configure map-keys-ordering

Set its severity in falcon.json. See the configuration guide.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "map-keys-ordering": "error"
      }
    }
  }
}

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/style/map-keys-ordering: <reason>