prefer-iterable-of
Same as prefer-iterable-of in Dart Code Metrics
- Group
- Style
- Path
lint/style/prefer-iterable-of- Default
- On (recommended)
- Origin
- Dart Code Metrics
- Rule
- 381 of 512
Flags .from collection constructors in favor of the .of constructor.
Catches List.from, Set.from, and Iterable.from — whether written as a
static call (List.from(xs), List<int>.from(xs)) or with new. The .from
constructor takes an Iterable<dynamic> and re-types its elements, so a type
mismatch surfaces only as a runtime cast failure; the .of constructor takes a
statically typed Iterable<E>, letting the compiler reject bad element types up
front. Prefer .of unless you specifically need .from's dynamic widening.
Invalid
void example(List<int> other, Set<String> names) {
final a = List<int>.from(other);
final b = Set<String>.from(names);
final c = List.from(other);
}
class Repo {
List<int> copy(Iterable<int> src) {
return List<int>.from(src);
}
void copySet(Iterable<String> items) {
final s = Set<String>.from(items);
}
Iterable<dynamic> copyIterable(Iterable<dynamic> data) {
return Iterable.from(data);
}
}
void nestedConstructors(List<int> values) {
final outer = List<int>.from(List.from(values));
final inner = List<int>.from(List.from(values));
}
// Legacy explicit `new` constructor form is still flagged.
void legacyNew(List<int> other, Set<String> names) {
final a = new List<int>.from(other);
final b = new Set.from(names);
final c = new Iterable.from(other);
}
// Regression: the violation must still be found inside Dart 3 containers
// (pattern declaration, labeled statement, switch expression and subject,
// collection if/spread, record field).
void containersRegression(int rcount, List<int> other) {
final (ra, _) = (List<int>.from(other), 0);
lbl: {
final rb = List<int>.from(other);
print(rb);
}
final rc = switch (rcount) {
0 => List<int>.from(other),
_ => null,
};
final rd = switch (List<int>.from(other)) {
_ => 0,
};
final re = [if (rcount > 0) List<int>.from(other)];
final rf = [...[List<int>.from(other)]];
final rg = (p: List<int>.from(other), q: 0);
print([ra, rc, rd, re, rf, rg]);
}void example(List<int> other, Set<String> names) { final a = List<int>.from(other); final a = List<int>.from(other); final b = Set<String>.from(names); final b = Set<String>.from(names); final c = List.from(other); List<int> copy(Iterable<int> src) { return List<int>.from(src); void copySet(Iterable<String> items) { final s = Set<String>.from(items); Iterable<dynamic> copyIterable(Iterable<dynamic> data) { return Iterable.from(data);void nestedConstructors(List<int> values) { final outer = List<int>.from(List.from(values));void nestedConstructors(List<int> values) { final outer = List<int>.from(List.from(values)); final outer = List<int>.from(List.from(values)); final inner = List<int>.from(List.from(values)); final outer = List<int>.from(List.from(values)); final inner = List<int>.from(List.from(values));void legacyNew(List<int> other, Set<String> names) { final a = new List<int>.from(other); final a = new List<int>.from(other); final b = new Set.from(names); final b = new Set.from(names); final c = new Iterable.from(other);void containersRegression(int rcount, List<int> other) { final (ra, _) = (List<int>.from(other), 0); lbl: { final rb = List<int>.from(other); final rc = switch (rcount) { 0 => List<int>.from(other), }; final rd = switch (List<int>.from(other)) { }; final re = [if (rcount > 0) List<int>.from(other)]; final re = [if (rcount > 0) List<int>.from(other)]; final rf = [...[List<int>.from(other)]]; final rf = [...[List<int>.from(other)]]; final rg = (p: List<int>.from(other), q: 0);Valid
void example(List<int> other, Set<String> names) {
final a = List<int>.of(other);
final b = Set<String>.of(names);
final c = List.of(other);
}
class Repo {
List<int> copy(Iterable<int> src) {
return List<int>.of(src);
}
void copySet(Iterable<String> items) {
final s = Set<String>.of(items);
}
Iterable<dynamic> copyIterable(Iterable<dynamic> data) {
return Iterable.of(data);
}
}
void spreadOperator(List<int> values) {
final list = [...values];
}
void mapFromIsOk() {
final map = Map.from({1: 'a', 2: 'b'});
}
void plainConstructors() {
final a = List<int>();
final b = Set<String>();
final c = Iterable<int>();
}
void newOfIsOk(List<int> other) {
final a = new List<int>.of(other);
final b = new Set.of(other);
}
void newNonIterableFromIsOk(Map<int, String> m) {
final a = new Map.from(m);
final b = new MyThing.from(m);
}Configure prefer-iterable-of
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"style": {
"prefer-iterable-of": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/style/prefer-iterable-of: <reason>More style rules
- prefer-iterable-where-type
prefer_iterable_whereTyperecommendedFlags
.where((e) => e is T), which.whereType<T>()expresses directly. - prefer-last
prefer-lastFlags manual last-element indexing in favor of the
.lastgetter. - prefer-match-file-name
Match a file's sole class or enum name to its snake-case basename.
- prefer-mixin
prefer_mixinrecommendedFlags
withclauses that mix in a plain class instead of a mixin. - prefer-multi-bloc-providerbloc
Flatten directly nested
package:flutter_blocBlocProviderswithMultiBlocProvider. - prefer-multi-providerprovider
Prefer a flat
MultiProviderto directly nested provider widgets. - prefer-named-boolean-parameters
Flags explicit positional
boolparameters in private declarations and unconstrained closures. - prefer-named-parameters
Prefer named parameters when a declaration exceeds
max-number(default 1).