在Dart中,List.from和List.of以及Map.from和Map.of之间有什么区别?他们的文件并不完全清楚:
/**
* Creates a [LinkedHashMap] instance that contains all key/value pairs of
* [other].
*
* The keys must all be instances of [K] and the values of [V].
* The [other] map itself can have any type.
*
* A `LinkedHashMap` requires the keys to implement compatible
* `operator==` and `hashCode`, and it allows `null` as a key.
* It iterates in key insertion order.
*/
factory Map.from(Map other) = LinkedHashMap<K, V>.from;
/**
* Creates a [LinkedHashMap] with the same keys and values as [other].
*
* A `LinkedHashMap` requires the keys to implement compatible
* `operator==` and `hashCode`, and it allows `null` as a key.
* It iterates in key insertion order.
*/
factory Map.of(Map<K, V> other) = LinkedHashMap<K, V>.of;
/**
* Creates a list containing all [elements].
*
* The [Iterator] of [elements] provides the order of the elements.
*
* All the [elements] should be instances of [E].
* The `elements` iterable itself may have any element type, so this
* constructor can be used to down-cast a `List`, for example as:
* ```dart
* List<SuperType> superList = ...;
* List<SubType> subList =
* new List<SubType>.from(superList.whereType<SubType>());
* ```
*
* This constructor creates a growable list when [growable] is true;
* otherwise, it returns a fixed-length list.
*/
external factory List.from(Iterable elements, {bool growable: true});
/**
* Creates a list from [elements].
*
* The [Iterator] of [elements] provides the order of the elements.
*
* This constructor creates a growable list when [growable] is true;
* otherwise, it returns a fixed-length list.
*/
factory List.of(Iterable<E> elements, {bool growable: true}) =>
new List<E>.from(elements, growable: growable);这种差异与仿制药有关吗?也许.from工厂允许您更改列表的类型,而.of工厂却没有?我来自Java背景,它与类型擦除一起工作,也许类型在Dart中被具体化了,您不能使用强制转换或原始类型来更改列表/映射类型?
发布于 2018-05-13 21:16:04
from和of方法的重要区别在于后者有类型注释,而前者没有。由于Dart泛型是具体化的,Dart 2是强类型的,这是确保正确构造List/Map的关键:
List<String> foo = new List.from(<int>[1, 2, 3]); // okay until runtime.
List<String> bar = new List.of(<int>[1, 2, 3]); // analysis error并确保正确推断类型:
var foo = new List.from(<int>[1, 2, 3]); // List<dynamic>
var bar = new List.of(<int>[1, 2, 3]); // List<int>在Dart 1中,类型是完全可选的,因此许多API是非类型化或部分类型化的。List.from和Map.from是很好的例子,因为传递给它们的Iterable/Map没有类型参数。有时候Dart可以知道这个对象应该是什么类型,但有时它只是作为List<dynamic>或Map<dynamic, dynamic>结束。
在Dart 2中,类型dynamic从顶部(对象)和底部(null)类型改为仅为顶级类型。因此,如果您在Dart 1中意外地创建了一个List<dynamic>,那么仍然可以将它传递给一个需要List<String>的方法。但在Dart 2中,List<dynamic>与List<Object>几乎相同,因此这将失败。
如果您使用的是Dart 2,则应该始终使用这些API的类型化版本。为什么旧的仍然存在,那里的计划是什么?我不太清楚。我想,随着时间的推移,他们将被逐步淘汰,就像其他的Dart 1一样。
发布于 2021-08-26 08:34:39
只要有可能,现在最好使用集合文本,而不是.from或.of构造函数。显然这有一些性能上的好处。(见底部的链接。)
示例:
something.toList()[...something]例外:
.from。但是,如果确实使用它们,则应该始终包括类型。
发布于 2022-09-07 07:14:40
List.of()和toList()
它们用于创建与原始列表相同类型的新列表,但List.of()可用于向上转换:
var ints = <int> [0];
var newList1 = ints.toList(); // List<int>
var newList2 = List<num>.of(ints); // List<num>还可以通过以下操作复制列表:
var newList3 = [...ints]; // List<int>
var newList4 = [for (var v in ints) v]; // List<int>List.from()
如果您想要向下转换,因此子类型是一种超级类型非常重要,请使用此方法。
var ints = List<int>.from(<num>[0, 1]); // Good as all elements are of type `int`
var ints = List<int>.from(<num>[0, 1.5]); // Bad as some elements are of type `double`https://stackoverflow.com/questions/50320220
复制相似问题