Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.3k views
in Technique[技术] by (71.8m points)

in dart, which is better List.generate or List.of or 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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)];

instead of

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

(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.

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.

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...