在 dart 中,Setclass 有两个不同的工厂构造函数Set.from()和Set.of(). 它们返回的结果是相同的类型。它们之间有用例区别吗?如何决定使用哪一个?
请参阅此程序的输出:
void main() {
List<int> myList = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5];
Set<int> mySetFrom = Set.from(myList);
Set<int> mySetOf = Set.of(myList);
print("mySetFrom type: ${mySetFrom.runtimeType}");
print("mySetFrom: ${mySetFrom}");
print("mySetOf type: ${mySetOf.runtimeType}");
print("mySetOf: ${mySetOf}");
}
Run Code Online (Sandbox Code Playgroud)
输出:
mySetFrom type: _CompactLinkedHashSet<int>
mySetFrom: {1, 2, 3, 4, 5}
mySetOf type: _CompactLinkedHashSet<int>
mySetOf: {1, 2, 3, 4, 5}
Run Code Online (Sandbox Code Playgroud)