首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Dart中,List.from和.of以及Map.from和.of之间有什么区别?

在Dart中,List.from和.of以及Map.from和.of之间有什么区别?
EN

Stack Overflow用户
提问于 2018-05-13 20:10:36
回答 3查看 7.9K关注 0票数 30

在Dart中,List.fromList.of以及Map.fromMap.of之间有什么区别?他们的文件并不完全清楚:

代码语言:javascript
复制
/**
* 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中被具体化了,您不能使用强制转换或原始类型来更改列表/映射类型?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-05-13 21:16:04

fromof方法的重要区别在于后者有类型注释,而前者没有。由于Dart泛型是具体化的,Dart 2是强类型的,这是确保正确构造List/Map的关键:

代码语言:javascript
复制
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

并确保正确推断类型:

代码语言:javascript
复制
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.fromMap.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一样。

票数 37
EN

Stack Overflow用户

发布于 2021-08-26 08:34:39

只要有可能,现在最好使用集合文本,而不是.from.of构造函数。显然这有一些性能上的好处。(见底部的链接。)

示例:

  • something.toList()
  • [...something]

例外:

  • 如果您需要下线,可以使用.from

但是,如果确实使用它们,则应该始终包括类型。

来源:https://stackoverflow.com/a/61541310/3681880

票数 2
EN

Stack Overflow用户

发布于 2022-09-07 07:14:40

List.of()toList()

它们用于创建与原始列表相同类型的新列表,但List.of()可用于向上转换:

代码语言:javascript
复制
var ints = <int> [0];
var newList1 = ints.toList(); // List<int>
var newList2 = List<num>.of(ints); // List<num>

还可以通过以下操作复制列表:

代码语言:javascript
复制
var newList3 = [...ints]; // List<int>
var newList4 = [for (var v in ints) v]; // List<int>

List.from()

如果您想要向下转换,因此子类型是一种超级类型非常重要,请使用此方法。

代码语言:javascript
复制
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`
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50320220

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档