typed_source_copied_with_from
Reports a collection copied with from out of elements that already have its type, as in List<int>.from(ints). Use of instead.
- Tier
- advisory
- Scope
- Dart
- Lowest preset
- recommended
- Fix
- writes
ofin place offrom
What it reports
Section titled “What it reports”A collection of the Dart libraries made with its from constructor out of elements that already have the type it holds: List<int>.from(ints), Set<num>.from(ints), Map<String, int>.from(prices), LinkedHashSet<int>.from(ids). The violation covers the constructor name.
It reports only where of gives a collection of the same type:
- the type arguments are written, and the source’s element type is a subtype of them; or
- they are inferred, and are exactly the source’s element type.
It does not report:
- a
dynamicsource, or one withdynamicelements:List<String>.from(json['tags']); - elements of a wider type, which
fromcasts at run time; - a collection whose inferred type arguments differ from the elements’:
final copy = List.from(ints)is aList<dynamic>, whileList.of(ints)would be aList<int>; - a class whose
fromandofdo not take the same arguments after the first.
from takes elements of any type and casts each as it copies, so a wrong element type shows only at run time. of takes elements of the collection’s type, which the compiler checks, and copies them without a cast. Where the elements have that type already, of says the same and checks more.
Example
Section titled “Example”List<int> copy(List<int> sizes) => List<int>.from(sizes);List<int> copy(List<int> sizes) => List<int>.of(sizes);Rule parameters
Section titled “Rule parameters”Set under the assay: key of the project’s root analysis_options.yaml.
| Rule parameter | Default | Effect |
|---|---|---|
enabled |
true |
false switches the rule off for the whole project. |
exclude |
[] |
Path globs where the rule is silent. |
Suppressing it
Section titled “Suppressing it”Write why on the line directly above the suppression:
// Why it stays as written here.// ignore: assay/typed_source_copied_with_from// ignore_for_file: assay/typed_source_copied_with_from silences it in a whole file, and // ignore: typed_source_copied_with_from without the prefix works too. See Suppressions.