You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Hint and/or lint for changing List/Set/Map from to of #58359
If we change List.from to List.of, the inferred type of key moves from dynamic to String. This would be helpful to the developer for completion suggestion, since key.isE• can now only complete to key.isEmpty, and not key.isEven.
We would want to suggest the replacement if the new inferred types do not lead to new or different errors or warnings or a different resolution of elements (e.g. a dynamic instance method call becoming a static extension method call).
@a14n: Yes, we should change that List.from to List.of. That code dates back to Dart 1 before there was a List.of, and the proposed hint would help find it.
@a14n: Regarding iterableOfE, less clear cut, so I would not suggest a similar hint.
They are different in that the run time type value E for toList comes from the object, and for List<E>.of from the context of the call.
I would use List.of if I wanted to update an element of the list; for lists that are not modified, toList is fine.
Reasons to use iterableOfE.toList():
It is more concise
Plays well with type infererence
It is in the right place when thinking of a chain like where-map-where-toList.
It can be a custom implementation, bound to the receiver, which can be more efficient that List.of. For this reason sometimes I wish List.of was implemented in terms of toList, but we can't because toList is less trustworthy (see below).
Reasons to use List.of:
You want to modify the list (e.g. assign a new element). toList might return a list with too narrow a type.
The compilers can do a more consistent good job with List.of because there is one implementation that is known to the compiler and the compiler knows the return type is one of the system lists, and how that depends on growable.
There are many implementations of toList, and some of them might be confusing for the compilers
The return type of toList has unconstrained variance, so it can return a subtype, e.g. => <Never>[];
The compilers can't always tell the exact implementation type(s) returned by toList, making handling of the returned list potentially polymorphic and less efficient.
Reacted by Alexandre Ardhuin, Phil Quitslund and Albert Mañosa
changed the title [-]Hint and/or lint for changing `List.from` to `List.of`[/-][+]Hint and/or lint for changing List/Set/Map `from` to `of`[/+]on Apr 26, 2025
Activity
pq commentedon Mar 24, 2021
Does the same advice extend to
Set.of?a14n commentedon Mar 24, 2021
Currious : is
List<E>.of(iterableOfE)better thaniterableOfE.toList()?If you look at https://api.dart.dev/stable/2.8.4/dart-core/Iterable/toList.html you can see that the implementation relies on
List.from. Should it be changed to useList.of?a14n commentedon Mar 24, 2021
And
Map.of?rakudrama commentedon Mar 24, 2021
The advice extends to
Set.ofandMap.of.@a14n: Yes, we should change that
List.fromtoList.of. That code dates back to Dart 1 before there was aList.of, and the proposed hint would help find it.@a14n: Regarding
iterableOfE, less clear cut, so I would not suggest a similar hint.They are different in that the run time type value
EfortoListcomes from the object, and forList<E>.offrom the context of the call.I would use
List.ofif I wanted to update an element of the list; for lists that are not modified,toListis fine.Reasons to use
iterableOfE.toList():List.of. For this reason sometimes I wishList.ofwas implemented in terms oftoList, but we can't becausetoListis less trustworthy (see below).Reasons to use
List.of:toListmight return a list with too narrow a type.List.ofbecause there is one implementation that is known to the compiler and the compiler knows the return type is one of the system lists, and how that depends ongrowable.toList, and some of them might be confusing for the compilerstoListhas unconstrained variance, so it can return a subtype, e.g.=> <Never>[];toList, making handling of the returned list potentially polymorphic and less efficient.pq commentedon Mar 24, 2021
We might also consider folding this advice into Effective Dart?
See: https://dart.dev/guides/language/effective-dart/usage#dont-use-listfrom-unless-you-intend-to-change-the-type-of-the-result
Levi-Lesches commentedon Jul 13, 2021
Isn't one of the main benefits of using
.fromthat you can cast the types more precisely?jamesderlin commentedon Jul 17, 2021
Seems like a duplicate of #58149?
Levi-Lesches commentedon Jul 18, 2021
Well that was created later than this, and the discussion is here, so technically #58149 is a duplicate of this
jamesderlin commentedon Jul 18, 2021
Huh? This issue seems to have been created in March 2021 (unless transferring the issue modified the timestamp?). #58149 was filed last year. =/
Levi-Lesches commentedon Jul 18, 2021
Oh sorry, I didn't see the years, just "March" and "April"
dnfield commentedon Dec 7, 2021
I'm seeing this issue in Flutter framework code - using from where we reallys hould use of because of extra type checking that is incurred by from.
srawlins commentedon Aug 3, 2022
And
Queue.from=>Queue.of.1 remaining item
srawlins commentedon Apr 26, 2025
See also #57106
use_of_instead_of_fromfor collections. #57106 as a duplicate of this issue[-]Hint and/or lint for changing `List.from` to `List.of`[/-][+]Hint and/or lint for changing List/Set/Map `from` to `of`[/+]Prefer .of over .from (#170750)
Prefer .of over .from (flutter#170750)
Prefer .of over .from (#170750)
Prefer .of over .from (#170750)