prefer-for-elements-to-map-from-iterable
Same as prefer_for_elements_to_map_fromIterable in Dart lints
- Group
- Complexity
- Path
lint/complexity/prefer-for-elements-to-map-from-iterable- Default
- On (recommended)
- Origin
- Dart lints
- Rule
- 359 of 512
Flags Map.fromIterable(x, key: ..., value: ...).
A collection-for element inside a map literal —
{ for (final e in x) key(e): value(e) } — expresses the same
transformation more directly, can be const, and avoids the intermediate
closures Map.fromIterable requires. The rule fires on both
Map.fromIterable(...) and new Map.fromIterable(...) when at least one
positional argument and both the key: and value: named arguments are
present.
Invalid
void bad() {
var a = Map.fromIterable(list, key: (e) => e, value: (e) => e * 2);
var b = Map.fromIterable(items, key: (k) => k.id, value: (v) => v.name);
var c = Map.fromIterable(nums, key: (n) => n.toString(), value: (n) => n);
var d = Map.fromIterable(data, value: (e) => e, key: (e) => e.hashCode);
var e = Map.fromIterable(xs, key: (x) => x, value: (x) => x);
}void bad() { var a = Map.fromIterable(list, key: (e) => e, value: (e) => e * 2); var a = Map.fromIterable(list, key: (e) => e, value: (e) => e * 2); var b = Map.fromIterable(items, key: (k) => k.id, value: (v) => v.name); var b = Map.fromIterable(items, key: (k) => k.id, value: (v) => v.name); var c = Map.fromIterable(nums, key: (n) => n.toString(), value: (n) => n); var c = Map.fromIterable(nums, key: (n) => n.toString(), value: (n) => n); var d = Map.fromIterable(data, value: (e) => e, key: (e) => e.hashCode); var d = Map.fromIterable(data, value: (e) => e, key: (e) => e.hashCode); var e = Map.fromIterable(xs, key: (x) => x, value: (x) => x);Valid
void good() {
var a = {for (final e in list) e: e * 2};
var b = Map.from(other);
var c = Map.of(existing);
var d = Map.fromEntries(entries);
var e = Map.fromIterables(keys, values);
var f = Map.fromIterable(list);
}Configure prefer-for-elements-to-map-from-iterable
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"complexity": {
"prefer-for-elements-to-map-from-iterable": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/complexity/prefer-for-elements-to-map-from-iterable: <reason>More complexity rules
- prefer-if-elements-to-conditional-expressions
prefer_if_elements_to_conditional_expressionsPrefers collection
ifelements to conditional expressions in list and set literals. - prefer-if-null-operators
prefer_if_null_operatorsrecommendedFlags
x == null ? y : x(and the invertedx != null ? x : y). - prefer-immediate-return
prefer-immediate-returnFlags a local variable that is returned on the immediately following statement.
- prefer-iterable-any
prefer_iterable_anyrecommendedFlags
iterable.where(...).isNotEmpty. - prefer-iterable-every
prefer_iterable_everyrecommendedFlags
iterable.where(predicate).isEmptyand filtered-length comparisons. - prefer-moving-to-variable
prefer-moving-to-variableFlags repeated property/getter chains in local variable initializers.
- prefer-null-aware-operators
prefer_null_aware_operatorsrecommendedFlags
x == null ? null : x.y(and the invertedx != null ? x.y : null). - unnecessary-const
unnecessary_constrecommendedFlags a redundant
constkeyword inside an already-const context.