在dart中,最好使用List.generate或List.of或List.from [英] in dart, which is better List.generate or List.of or List.from

查看:2009
本文介绍了在dart中,最好使用List.generate或List.of或List.from的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建列表的三个可迭代选项。哪个最适合什么目的,哪个更有效?

in Dart Documentation we have three iterable options to choose from to create lists .which one is best suited for what purpose and which is more effiecient for what purpose?

推荐答案

基本上,您永远不要使用任何这些构造函数。

Basically, you should never use any of those constructors.

每种都有其用途,但是其中大多数用途现在都可以使用列表文字来编写。

Each has its use, but most of those uses can be written using list literals now.

List.generate List.filled 的项坠。后者创建一个在每个插槽中填充相同值的列表,前者允许您为每个插槽计算一个新值。
对于collection-,我可能会这样写:

The List.generate is a pendant to List.filled. The latter creates a list filled with the same value in each slot, the former allows you to compute a new value for each slot. With collection-for, I'd probably write:

 var newList = [for (var i = 0; i < 10; i++) compute(i)];

而不是

 var newList = List.generate(10, compute);

(甚至更多,如果我可以内嵌 compute 函数)。
generate 有意义的一种情况是创建一个固定长度的列表。文字不能做到这一点。

(even more so if I can inline the compute function). The one case where generate makes sense is to create a fixed-length list. The literal cannot do that.

我还要说你永远不要使用 List.of(something)
使用 something.toList() [... something] 代替。如果您需要上播,例如说从 Iterable< int> 创建 List< num> < num> [...某物] ,但是您不能使用 toList 。如果您需要制作一个定长 List< num> ,...那么我认为 List< num> .of(某些东西,可增长:false)实际上是最简单的解决方案。

I'd also say that you should never use List.of(something). Use something.toList() or [...something] instead. If you need to up-cast, say create a List<num> from an Iterable<int>, you can do <num>[...something], but you can't use toList. If you need to make a fixed-length List<num>, ... then I think List<num>.of(something, growable: false) is actually the simplest solution.

使用 List.from 的唯一原因是当原始可迭代对象没有所需的紧密类型时。如果您知道 Iterable< num> 仅包含整数,则可能要执行 List< int> .from(iterable)。您也可以将 iterable.cast< int>()。toList() [对于(var v in iterable)v as int] ,但 List.from 可以更短。 总是 List.from 提供类型变量。

The only reason to use List.from is when the original iterable does not have as tight a type as needed. If you know that your Iterable<num> contains only integers, you might want to do List<int>.from(iterable). You can also do iterable.cast<int>().toList() or [for (var v in iterable) v as int], but List.from can be shorter. Always provide a type variable to List.from.

因此,通常:仅在需要固定长度列表(通过 growable:false )或要使用 List.from ,然后始终提供类型参数。

So, in general: Only use one of these constructors if you need a fixed-length list (passing growable: false), or if you want to down-cast the elements using List.from, and then always provide the type argument.

否则,请使用列表文字。这也可能会更有效率,因为它避免了一些函数调用。

Otherwise use list literals. That's probably going to be more efficient too because it avoids some function calls.

这篇关于在dart中,最好使用List.generate或List.of或List.from的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆