有没有一种简单的方法可以在Dart中合并两个列表? [英] Is there a simple way to combine two lists in Dart?

查看:553
本文介绍了有没有一种简单的方法可以在Dart中合并两个列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种简单的方法可以在dart中组合两个列表以创建一个全新的列表对象.我找不到任何东西,像这样:

I was wondering if there was an easy way to combine two lists in dart to create a brand new list object. I couldn't find anything and something like this:

var newList = list1 + list2;

无效.

推荐答案

您可以使用:

var newList = new List.from(list1)..addAll(list2);

如果您有多个列表,则可以使用:

If you have several lists you can use:

var newList = [list1, list2, list3].expand((x) => x).toList()

从Dart 2开始,您现在可以使用+:

As of Dart 2 you can now use +:

var newList = list1 + list2 + list3;

从Dart 2.3开始,您可以使用传播运算符:

As of Dart 2.3 you can use the spread operator:

var newList = [...list1, ...list2, ...list3];

这篇关于有没有一种简单的方法可以在Dart中合并两个列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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