Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
List 在Dart中,什么';从列表到的和从地图到的之间的区别是什么?_List_Dictionary_Generics_Types_Dart - Fatal编程技术网

List 在Dart中,什么';从列表到的和从地图到的之间的区别是什么?

List 在Dart中,什么';从列表到的和从地图到的之间的区别是什么?,list,dictionary,generics,types,dart,List,Dictionary,Generics,Types,Dart,在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 hav

在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);
/**
*创建一个[LinkedHashMap]实例,该实例包含
*[其他]。
*
*所有键都必须是[K]的实例和[V]的值。
*[其他]映射本身可以具有任何类型。
*
*“LinkedHashMap”需要密钥来实现兼容
*`operator=`和`hashCode`,它允许`null`作为键。
*它以键插入顺序迭代。
*/
工厂映射.from(映射其他)=LinkedHashMap.from;
/**
*使用与[other]相同的键和值创建[LinkedHashMap]。
*
*“LinkedHashMap”需要密钥来实现兼容
*`operator=`和`hashCode`,它允许`null`作为键。
*它以键插入顺序迭代。
*/
工厂地图(其他地图)=LinkedHashMap.of;
/**
*创建包含所有[元素]的列表。
*
*[elements]的[Iterator]提供元素的顺序。
*
*所有[Element]都应该是[E]的实例。
*'elements`iterable本身可以有任何元素类型,因此
*构造函数可用于向下转换“列表”,例如:
*“飞镖
*列表超级列表=。。。;
*列表子列表=
*新的List.from(superList.whereType());
* ```
*
*当[Growtable]为true时,此构造函数创建一个可增长列表;
*否则,它将返回一个固定长度的列表。
*/
外部工厂列表.from(Iterable元素,{bool growtable:true});
/**
*从[元素]创建列表。
*
*[elements]的[Iterator]提供元素的顺序。
*
*当[Growtable]为true时,此构造函数创建一个可增长列表;
*否则,它将返回一个固定长度的列表。
*/
工厂列表(Iterable元素,{bool growtable:true})=>
新列表。从(元素,可增长:可增长);

这种差异与泛型有关吗?也许来自工厂的
。允许您更改列表的类型,而来自
工厂的
则不允许您更改列表的类型?我来自Java背景,可以使用类型擦除,可能类型在Dart中被具体化,您不能使用强制转换或原始类型来更改列表/映射类型?

from
方法的
方法的重要区别在于后者有类型注释,而前者没有。由于Dart泛型是具体化的,而Dart 2是强类型的,这对于确保正确构造
列表/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
List foo=new List.from([1,2,3]);//直到运行时。
列表栏=新列表([1,2,3]);//分析误差
并确保正确推断类型:

var foo = new List.from(<int>[1, 2, 3]); // List<dynamic>
var bar = new List.of(<int>[1, 2, 3]); // List<int>
var foo=new List.from([1,2,3]);//列表
var bar=新列表([1,2,3]);//列表
在Dart 1中,类型是完全可选的,因此许多API是非类型化或部分类型化的
List.from
Map.from
都是很好的例子,因为传递给它们的
Iterable/Map
没有类型参数。有时Dart可以确定该对象的类型,但有时它只是以
List
Map
结束

在Dart 2中,类型
dynamic
从顶部(对象)和底部(空)类型更改为仅顶部类型。因此,如果您在Dart 1中意外创建了一个
列表
,您仍然可以将其传递给需要
列表
的方法。但是在Dart 2中,列表
几乎与列表
相同,因此这将失败


如果您使用的是Dart 2,则应始终使用这些API的类型化版本。为什么旧的仍然存在,那里有什么计划?我真的不知道。我猜随着时间的推移,它们将与Dart 1的其余部分一起被淘汰。

可能是
列表。如果您想放弃类型信息,from
很有用吗?与
List.from
文档中给出的示例类似:
List子列表=新列表.from(superList.whereType())。这不能用
List.of
?你可以用
List.of做的一切。但是从
你可以用
List.of
做。在本例中,List sublist=new List.of(superList.whereType()),则应更新方法文档。一点也不清楚。谢谢。值得提出一个关于dart sdk回购的问题,但我也认为样式指南将从2.0版更新。这些构造函数的存在可能只是为了简化迁移