Closed
Description
This tracker is for issues related to:
- Dart core libraries (
dart:async
,dart:io
, etc.)
Some other pieces of the Dart ecosystem are maintained elsewhere.
Please file issues in their repository:
- Dart language: https://github.com/dart-lang/language
Let's assume I have such code:
void main() {
final holder = Holder<B>(contents: [Content(data: B())]);
test(holder);
}
sealed class A {}
class B extends A {}
class C extends A {}
class Content<S extends A> {
final S data;
Content({required this.data});
}
class Holder<S extends A> {
final List<Content<S>> contents;
Holder({required this.contents});
}
So if I use toList()
I will get TypeError
:
void test(Holder holder) {
final contents = holder.contents.toList();
contents.add(Content(data: B())); // TypeError
}
But List.of()
doesn't produce that error:
void test(Holder holder) {
final contents = List.of(holder.contents);
contents.add(Content(data: B())); // Okay
}
I wonder why? toList()
under the hood uses List.of()
but the result is different. List.from
and [...contents]
cause no problem, only toList()
does.
Also that will work:
void test(Holder holder) {
final contents = holder.contents.map((e) => e)).toList();
contents.add(Content(data: B())); // Okay
}