在Dart中,List.fromList.ofMap.fromMap.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中被具体化,而您不能使用强制转换或原始类型来更改列表/映射类型?

推荐答案

fromof方法之间的重要区别在于后者有类型批注,而前者没有.由于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.fromMap.from是很好的例子,因为传递给它们的Iterable/Map没有类型参数.有时DART可以计算出该对象的类型,但有时结果是List<dynamic>Map<dynamic, dynamic>.

在DART 2中,类型dynamic从顶部(对象)和底部(空)类型都改变为仅是顶部类型.因此,如果您在DART 1中意外创建了List<dynamic>,您仍然可以将其传递给需要List<String>的方法.但在DART 2中,List<dynamic>几乎与List<Object>相同,因此这将失败.

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

Dart相关问答推荐

如何映射作为DART中其他模型基础的复杂模型

在异步方法中,有没有更优雅的方式在DART中等待!=NULL?

如何将指针事件发送到堆栈中的低级子级

替换 Flutter 中的片段等小部件

如何修复 Flutter 中的Checking Dart SDK version... << was unexpected at this time错误?

Flutter插件和Flutter模块之间有什么区别?

在 Dart 中向服务器发出多个独立请求的最佳方式

如何使用flatter删除Firestore文档中的字段

如何使 FadeInImage 循环?

Flutter 中 ListView.builder 中的反向列表

Dart 中的动态类方法调用

Dart 2:Future 和 Future 之间的区别

带有 Dart 的 CORS,我如何让它工作?

在 Dart 中将类类型作为变量传递

用 dart 解析日期

Dart 转换列表的每个元素

dart中是否有类似struct struct 的东西?

比较在 Dart 中创建单例的方法

Dart,对泛型的限制?

具有 Maps 的 Null 感知运算符