[英]How to return an immutable List in Dart?
So in other languages there are ArrayList
or MutableList
which allows modifications (add, delete, remove) to list items.因此在其他语言中有
ArrayList
或MutableList
允许修改(添加、删除、删除)列表项。 Now to avoid modification of these list, simply return the MutableList
or ArrayList
as a List
.现在为了避免修改这些列表,只需将
MutableList
或ArrayList
作为List
返回。
I want to do the same thing in Dart
.我想在
Dart
中做同样的事情。 But in Dart
returning a List
still allows you to do list.add
.但是在
Dart
中返回一个List
仍然允许你做list.add
。 How can this be done properly in Dart?如何在 Dart 中正确完成此操作?
There is no type for unmodifiable lists in Dart, just the List
type. Dart 中没有不可修改列表的类型,只有
List
类型。 Some List
implementations accept calling add
, others do not.一些
List
实现接受调用add
,而另一些则不接受。
You can return an actually unmodifiable list, say created using List.unmodifiable
, as a List
.您可以返回一个实际不可修改的列表,例如使用
List.unmodifiable
创建的List
。 The user will get a run-time error if they try to call add
on it.如果用户尝试在其上调用
add
,则会收到运行时错误。
As Irn said, there is no type for immutable lists in Dart, but this supplemental answer shows examples of creating immutable lists.正如 Irn 所说,Dart 中没有不可变列表的类型,但这个补充答案显示了创建不可变列表的示例。 You can't add, remove, or modify the list elements.
您不能添加、删除或修改列表元素。
Use the const
keyword to create the list.使用
const
关键字创建列表。
const myList = [1, 2, 3];
Note : Adding the optional const
keyword before the list literal is redundant when the variable is already const
.注意:当变量已经是
const
时,在列表文字之前添加可选的const
关键字是多余的。
const myList = const [1, 2, 3];
If the variable can't be a const
, you can still make the value be const
.如果变量不能是
const
,您仍然可以将值设为const
。
final myList = const [1, 2, 3];
If you don't know what the list elements are until runtime, then you can use the List.unmodifiable()
constructor to create an immutable list.如果您直到运行时才知道列表元素是什么,那么您可以使用
List.unmodifiable()
构造函数来创建一个不可变列表。
final myList = List.unmodifiable([someElement, anotherElement]);
To prevent modifications on your list, just use a UnmodifiableListView
from the built-in dart:collection library:为了防止修改列表,只需使用内置dart:collection库中的
UnmodifiableListView
:
import 'package:collection/collection.dart';
List<int> protectedNumbersUntil4() {
return UnmodifiableListView([0, 1, 2, 3, 4]);
}
final numbers = protectedNumbersUntil4();
numbers.add(5); // throws
You may use Iterable<type>
.您可以使用
Iterable<type>
。 It is not a List<type>
, it does not provide methods to modify, but it does to iterate.它不是
List<type>
,它不提供修改方法,但它提供迭代。 It also provides a .toList()
method, if needed.如果需要,它还提供
.toList()
方法。 Depending on your construction, it may be better to use Iterable
instead of List
to ensure consistency.根据您的构造,最好使用
Iterable
而不是List
以确保一致性。
# Almost good:
final Iterable<int> foo = [1, 2, 3];
foo.add(4); # The method .add isnt defined.
(foo as List<int>).add(4); # Would work.
print(foo); # [1,2,3,4]
Even though foo
is instantiated as a mutable List
, the interface only says it is a Iterable
.即使
foo
被实例化为一个可变的List
,接口只说它是一个Iterable
。
# Good:
final Iterable<int> foo = List.unmodifiable([1, 2, 3]);
foo.add(4); # The method .add isnt defined.
(foo as List<int>).add(4); # Uncaught Error: Unsupported operation: add.
foo.toList().add(4); # Works but it is OK because it does not mutates foo as foo.toList() returns a separate instance.
print(foo); # [1,2,3]
You should use IList
an immutable list from package:darts/dartz.dart
the next best solution would be to use KtList
(which is an immutable Kotlin collection), both are immutable and will serve as immutable lists, however, I prefer KtList
as it is much easier to use. You should use
IList
an immutable list from package:darts/dartz.dart
the next best solution would be to use KtList
(which is an immutable Kotlin collection), both are immutable and will serve as immutable lists, however, I prefer KtList
as it更容易使用。
Also, check out this medium post on immutability ( https://medium.com/dartlang/an-intro-to-immutability-with-dart-d4de871865c7 )另外,查看这篇关于不变性的中等帖子( https://medium.com/dartlang/an-intro-to-immutability-with-dart-d4de871865c7 )
And this one on ktlist https://medium.com/flutter-community/kt-dart-better-collections-for-your-flutter-business-logic-41886ab7883这个在 ktlist https://medium.com/flutter-community/kt-dart-better-collections-for-your-flutter-business-logic-41886ab7883
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.