【发布时间】:2021-11-14 13:20:33
【问题描述】:
我无法找到答案。希望你们中的任何人都可以解释除了其中一个看起来更漂亮之外是否有任何区别。
假设我有一个字符串列表(只是一个例子,任何列表用例都适合)
final strings = ['string 1', 'string 2', 'string 3'];
我想在一些 Text 小部件中呈现这些字符串。
用for循环来做,像这样:
Column(
children: [
for(final string in strings)
Text(string),
],
);
或者用地图做,像这样:
Column(
children: strings.map((String string) => Text(string)).toList()
);
在性能或其他方面有什么不同吗?
【问题讨论】:
-
简短的回答是肯定的。从技术上讲 map 有点慢,但实际上你的应用程序不可能扩展到一个明显的点。在你达到那个点之前,Flutter 会窒息很久。我总是更喜欢
map,因为它保留了功能组合并且更易于阅读 imo
标签: flutter dictionary for-loop dart iterator