In this guide, we’ll learn how to combine lists in Dart. We’ll be using multiple easy Dart code examples to practically demonstrate how to merge/combine two or more lists.
After reading this post, you’ll have a detailed practical knowledge of how to easily combine lists in Dart.
So without any delay, let’s just get right into its practical implementation.
What is Combine Lists in Dart?
It means merging lists with each other. We’ll be going through 4 ways through which we can combine lists in Dart programming language. Click here if you want to learn more about Dart lists.
So let’s start understanding it using proper practical code examples.
4 Ways To Combine Lists in Dart (Easy Examples)
These 4 ways/methods are listed below:
- Using addAll() Method
- Using + Operator
- Using Spread Operator
- Using expand() Method
Using addAll() Method
This method is used to add all the elements of second list to the first list. Syntax is given below:
Let’s now understand it with a practical example. See below code:
List listofIntegers=[1,2,3,4,5]; List listOfDouble=[3.4,2.2,3.1,4.8,5.9]; listofIntegers.addAll(listOfDouble); //it will merge list of double to list of int print(listofIntegers);
[1, 2, 3, 4, 5, 3.4, 2.2, 3.1, 4.8, 5.9]
We have successfully merged the second list into first one. Second list is added after the last item of first list.
Using + Operator
We can also use an addition operator to combine lists in Dart. See below code:
List listofIntegers=[1,2,3]; List listOfDouble=[3.4,2.2,3.1,4.8]; List newList= listofIntegers + listOfDouble; print(newList);
[1, 2, 3, 3.4, 2.2, 3.1, 4.8]
We’ve merged these two Dart lists and as you can see, we’ve printed our merged list by assigning the result of these 2 lists to a new list. You can print it directly without assigning the result to a new list.
Using Spread Operator
We can easily combine lists in Dart by using spread operator. See below example:
List listofIntegers=[1,2,3]; List listOfDouble=[3.4,2.2,3.1,4.8]; List listOfString=['White','Green','Blue','Purple','Grey']; List newList= [ ...listofIntegers, ...listOfDouble, ...listOfString ]; print(newList);
[1, 2, 3, 3.4, 2.2, 3.1, 4.8, White, Green, Blue, Purple, Grey]
The lists are successfully merged using the spread operator.
Using expand() Method
We can also use this method to combine lists in Dart. Its an easy method if you want to combine two or more lists. See below code:
List listofIntegers=[1,2,3]; List listOfDouble=[3.1,4.8,3.4,2.2]; List listOfString=['White','Purple','Grey','Green','Blue']; List newList= [listofIntegers, listOfDouble, listOfString].expand( (x)=>x ).toList(); print(newList);
[1, 2, 3, 3.1, 4.8, 3.4, 2.2, White, Purple, Grey, Green, Blue]
This is how you can easily combine two or more lists using expand method. It’ll take each item of the provided lists, starting from the items of first list, then second list and so on until all the items of lists are fetched. Then we’ve to use the to list method and the result will be a new merged list.
So this is how we can easily combine lists in Dart. Hope you have learned alot from it. Do give these methods a try and share your experience with us.
Do ask if you still have any questions related to this topic. I’ll be very happy to answer all your questions.
To conclude, hope you now have a proper and detailed practical knowledge of how to combine lists in Dart using multiple methods. I’ll be delighted to have your feedback on this post.
I’d also highly recommend you to read my other articles as well. Thank you for reading this post.