dart list & it's inbuilt method
dart list

Before using Dart List, you should be aware of the following details:

  • Dart supports two types of lists: fixed-length lists and growable lists.
  • The dart list must allow for duplicate values.
  • Dart list is an ordered collection that keeps the insertion order of the items.
  • While an operation on the list is being performed, changing the length of the list will break the operation.

What exactly is a list in dart language

The List datatype in Dart programming is similar to arrays in other programming languages. A list is used to represent a group of objects. It is an ordered collection of objects. Dart’s core libraries are responsible for the existence, creation, and manipulation of the List class, An ordered collection of objects.

In a Dart how do you define a list?

The Dart list resembles JavaScript array literals. The syntax for declaring the list is as follows. All elements in the Dart list are stored inside square brackets ([]) and separated by commas (,).

Syntax of a Dart List:

Dart in List is defined using this [] .

List listName = [1,2,3,4,5];

Dart List Properties

1. First

The First element of the list is returned by this method.

Syntax : List.first

void main() {
  List listName = [6,1,2,3,4,5];
  listName.first;
  print("$listName .first is the List's first element");
}

Your desired OutPut show below :
6 is the List's first element.

2. Last

The Last element of the list is returned by this method.

Syntax : List.last

void main() {
  List listName = [6,1,2,3,4,5];
  listName.last;
  print("$listName.last is the List's first element");
}

Your desired OutPut show below :
5 is the List's first element.

3. isEmpty

If the collection has at least one element, it returns false.

Syntax : List.isEmpty

void main() {
  List listName = [];
  print("List is Empty : ${listName.isEmpty}");
}

Your desired OutPut show below :

List is Empty : true

void main() {
  List listName = [6,2,3,4,5];
  print("List is Empty : ${listName.isEmpty}");
}

Your desired OutPut show below :
List is Empty : false

4. isNotEmpty

If the collection has at least one element, this method returns true.

Syntax: List.isNotEmpty

void main() {
  List listName = [];
  print("List is NotEmpty : ${listName.isNotEmpty}");
}

Your desired OutPut show below :
List is NotEmpty : false

void main() {
  List listName = [];
  listName.add(12);
  print("List is NotEmpty : ${listName.isNotEmpty}");
  print("List value is : ${listName}");
}

Your desired OutPut show below :
List is NotEmpty : true
List value is : [12]

5. Length

Returns the size of the list.

Syntax: List.length

void main() {
  List listName = [6, 1, 2, 3, 4, 5];
  print("The list's length is ${listName.length}");
}

Your desired OutPut show below :
The list's length is 6

6. reversed

This method produces an iterable object containing the contents of the list in reverse order.

Syntax : List.reversed

void main() {
  List listName = [6, 1, 2, 3, 4, 5];
  print("The reverse order of the list is : ${listName.reversed}");
}

Your desired OutPut show below :
The reverse order of the list is : (5, 4, 3, 2, 1, 6)

7. single

Check the list and return a single element if there is just one in the list.

Syntax: List.single

void main() {
  List listName = [6];
  print("There is only one item in the list, and its value is : $listName.single.");
}

Your desired OutPut show below :
There is only one item in the list, and its value is : 6

Insert element in the Dart List

Dart supports a variety of techniques for adding items to lists, which are used to insert elements into lists.

These techniques are listed below.

  1. add()
  2. addAll()
  3. insert()
  4. insertAll()

Let’s go through this one by one and check how to add element’s into dart list.

add()

This add() function adds the supplied value to the end of the list. It can only add one entry at a time and then return the changed list object.

Syntax : listName.add(element)

void main() {
  List listName = [1, 2, 3, 4, 5];
  print("The list element's value is : ${listName}");
  listName.add(12);
  print("After the list value, add the : ${listName}");
}

Your desired OutPut show below :
The list element's value is : [1, 2, 3, 4, 5]
After the list value, add the : [1, 2, 3, 4, 5, 12]

addAll()

This addAll() function is used to insert numerous values into the supplied list. Each value is separated by commas and contained by a square bracket ([value]).

Syntax : listName.addAll([v1,v2,v3,v4,v5,…….n]);

void main() {
  List listName = [1, 2, 3, 4, 5];
  print("List element value is :${listName}");
  listName.addAll([12, 13, 14, 15, 16]);
  print("List an addAll the element after the list value is :${listName}");
}

Your desired OutPut show below :
List element value is :

[1, 2, 3, 4, 5]

List an addAll the element after the list value is :

[1, 2, 3, 4, 5, 12, 13, 14, 15, 16]

insert()

This insert() function is used to add a list entry at a specific index. The index position of the value to be added in the list can be specified.

Syntax : listName.insert(index,value);  

The syntax in the index variable assigns the specific index value that you must assign, and value into set specific index into desired value.

void main() {
  List listName = [1, 2, 3, 4, 5];
  print("List element value is :${listName}");
  listName.insert(2, 100);
  print("List element after insert the element :${listName}");
}

Your desired OutPut show below :
List element value is :

[1, 2, 3, 4, 5]
List element after insert the element :
[1, 2, 100, 3, 4, 5]

insertAll()

insertAll() is used to insert multiple values from the specified index point. It accepts an index position and a list of values as arguments and insert all the item into the list from specified index point.

Syntax :  listName.insertAll(index,listOfValue)

The syntax in index specifies a certain list of index values, and list of values specifies this type of [] list.

void main() {
  List listName = [1, 2, 3, 4, 5];
  print("List element value is :${listName}");
  listName.insertAll(0, [6, 7, 8, 9, 10]);
  print("List contains item after update  :${listName}");
}

Your desired OutPut show below:
List element value is :

[1, 2, 3, 4, 5]
List contains item after update:

[6, 7, 8, 9, 10, 1, 2, 3, 4, 5]

Dart update list item

The dart programming language has an Update list, which we may edit by simply accessing its element and assigning a new value at that position.

Syntax :  listName[index]=newValue;

You may also use the following properties to update one or more items in a List:

The item’s index

Eg:

void main() {
  List listName = [1, 2, 3, 4, 5];
  print("List contains item before update :${listName}");
  listName[3] = 100;
  print("List contains item after update  :${listName}");
}

Your desired OutPut show below :
List contains item before update :  [1, 2, 3, 4, 5]
List contains item after update  :  [1, 2, 3, 100, 5]

replaceRange

To delete items from a range, use the replaceRange() function, then insert others.

Eg:

void main() {
  List listName = [1, 2, 3, 4, 5];
  print("List contains item before update :${listName}");
  listName.replaceRange(1, 2, [1000]);
  print("List contains item after update  :${listName}");
}

Your desired OutPut show below :
List contains item before update : [1, 2, 3, 4, 5]
List contains item after update  : [1, 1000, 3, 4, 5]

Remove the list elements

Dart offers the following methods for removing list items:

  1. remove()
  2. removeAt()
  3. removeLast()
  4. removeRange()

Let’s use the above method and learn how to remove element from dart list.

remove()

removes() function removes one entry from the provided list at a time. As an argument, it accepts elements. If there are several occurrences of the supplied element in the list, it eliminates the first occurrence.

Syntax : listName.remove(value);  

void main() {
  List listname = [1, 2, 3, 4, 5];
  print("List contains item before remove list item :${listName}");
  listName.remove(5);
  print("List contains item after remove list item  :${listName}");
}

Your desired OutPut show below :
List contains item before remove list item : [1, 2, 3, 4, 5]
List contains item after remove list item  : [1, 2, 3, 4]

removeAt()

It returns an element that was removed from the provided index point.

Syntax : listName.removeAt(int index); 

void main() {
  List listName = [1, 2, 3, 4, 5];
  print("Before removing an item from the list, Item in the list : ${listName}");
  listName.removeAt(3);
  print("After removing an item, the list includes it. At the top of the list :           ${listName}");
}

Your desired OutPut show below :
Before removing an item from the list, Item in the list : [1, 2, 3, 4, 5]
After removing an item, the list includes it. At the top of the list : [1, 2, 3, 5]

removeLast()

The removeLast() function is used to remove the list’s last element.

Syntax : listName.removeLast; 

void main() {
  List listName = [1, 2, 3, 4, 5];
  print("List contains item before removeLast list item :${listName}");
  listName.removeLast();
  print("List contains item after removeLast list item  :${listName}");
}

Your desired OutPut show below :
List contains item before removeLast list item :[1, 2, 3, 4, 5]
List contains item after removeLast list item  :[1, 2, 3, 4]

removeRange()

The item inside the provided range is removed using the removeRange() function. It takes two arguments: start and end indexes.

Syntax : listName.removeRange(startIndex ,endindex);

void main() {
  List listName = [1, 2, 3, 4, 5];
  print("Before deleting an element, the list includes the following item : ${listName}");
  listName.removeRange(1, 3);
  print("After removing the element, the list contains the following item : ${listName}");
}

Your desired OutPut show below :
Before deleting an element, the list includes the following item : [1, 2, 3, 4, 5]
After removing the element, the list contains the following item : [1, 4, 5]

How to shuffle a list in dart

shuffle()

Shuffles a list randomly. By giving start and end, a sub-range of the list can be shuffled; if start and end are omitted, they default to the list’s start and finish. If random is not specified, a new instance of the random variable is created.

The dart list shuffle() function reorganizes the order of elements in a sequence, such as a list. The shuffle() function returns the modifications to the existing list rather than a new list.

Syntax : listName.shuffle();

void main() {
  List listName = [1, 2, 3, 4, 5];
  print("List before using shuffle :${listName}");
  listName.shuffle();
  print("List after using shuffle :${listName}");
}

Your desired OutPut show below :
List before using shuffle :[1, 2, 3, 4, 5]
List after using shuffle :[4, 2, 1, 3, 5]

How to Convert List to Map in dart

asMap()

This function asMap() returns a map representation of the supplied list. The indices would be the key, and the list components would be the value.

Eg:

void main() {
  List<int> listName = [1, 2, 3, 4, 5];
  print("List before convert asMap :${listName}");

  Map<int, int> convertToMap = listName.asMap();
  print("List after convert asMap :${convertToMap}");
}

Your desired OutPut show below :
List before convert asMap :[1, 2, 3, 4, 5]
List after convert asMap :{0: 1, 1: 2, 2: 3, 3: 4, 4: 5}

take()

This take() function returns an iterable starting at index 0 and ending at the count specified from the given list.

Syntax : listName.take(value);  

void main() {
  List listName = [1, 2, 3, 4, 5];

  print("List Contains value :${listName}");
  print("List Contains specific value :${listName.take(2)}");
}

Your desired OutPut show below :
List Contains value : [1, 2, 3, 4, 5]
List Contains specific value : (1, 2)

skip()

This skip() function skips the elements of the list beginning at index 0 and ending at count and returns the remaining iterable from the provided list.

Syntax : listName.skip(value);  

void main() {
  List listName = [1, 2, 3, 4, 5];
  
  print("ListConatins value :${listName}");
  print("List Contains skipped value :${listName.skip(2)}}");
}

Your desired OutPut show below :
ListConatins value :[1, 2, 3, 4, 5]
List Contains skipped value :(3, 4, 5)

contains()

Search for specific value in list

Determine whether the provided element is present in the list and return the bool value.

Syntax : listName.contains(value);  

void main() {
  List listName = [1, 2, 3, 4, 5];

print("List Contains value :${listName}");
  print("List check Contains value :${listName.contains(2)}");
}

Your desired OutPut show below :
List Contains value :[1, 2, 3, 4, 5]
List check Contains value :true

Recommended Articles

How to create a Reorderable listview in flutter

Arrays in dart (LIST)