Debug School

rakesh kumar
rakesh kumar

Posted on

Dart Programming - Lists

Dart Programming - Lists

Dart represents arrays in the form of List objects. A List is simply an ordered group of objects. The dart:core library provides the List class that enables creation and manipulation of lists.
Lists can be classified as −


Fixed Length List
Growable List

Fixed Length List
A fixed length list’s length cannot change at runtime. The syntax for creating a fixed length list is as given below −

Step 1 − Declaring a list

The syntax for declaring a fixed length list is given below −


var list_name = new List(initial_size)

The above syntax creates a list of the specified size. The list cannot grow or shrink at runtime. Any attempt to resize the list will result in an exception.
Step 2 − Initializing a list

The syntax for initializing a list is as given below −


lst_name[index] = value;

Example

void main() { 
   var lst = new List(3); 
   lst[0] = 12; 
   lst[1] = 13; 
   lst[2] = 11; 
   print(lst); 
}
Enter fullscreen mode Exit fullscreen mode

`
It will produce the following output −

[12, 13, 11]
`
Growable List
A growable list’s length can change at run-time. The syntax for declaring and initializing a growable list is as given below −

Step 1 − Declaring a List


var list_name = [val1,val2,val3]

--- creates a list containing the specified values
OR
`

var list_name = new List()

--- creates a list of size zero
Step 2 − Initializing a List

The index / subscript is used to reference the element that should be populated with a value. The syntax for initializing a list is as given below −

list_name[index] = value;

Example
The following example shows how to create a list of 3 elements.


void main() {
var num_list = [1,2,3];
print(num_list);
}

It will produce the following output −

[1, 2, 3]

Example
The following example creates a zero-length list using the empty List() constructor. The add() function in the List class is used to dynamically add elements to the list.


void main() {
var lst = new List();
lst.add(12);
lst.add(13);
print(lst);
}

It will produce the following output −

[12, 13]

Summary
Type
Declaring a list
Initializing a List


Dart Programming - ListFirst Method

Syntax
List.first

Example


void main() {
var lst = new List();
lst.add(12);
lst.add(13);
print("The first element of the list is: ${lst.first}");
}

It will produce the following output −.

The first element of the list is: 12

Summary
Type
Syntax
Example


Dart Programming - List isEmpty Method


Syntax
List.isEmpty

Example


void main() {
var lst = new List();
lst.add(12);
lst.add(13);
print(lst.isEmpty);
}

It will produce the following output −.


False

Summary
Type
Syntax

Example

Dart Programming - List.isNotEmpty Method

Syntax


List.isNotEmpty

Example


void main() {
var lst = new List();
lst.add(12);
lst.add(13);
print(lst.isNotEmpty);
}

It will produce the following output −.


true

Summary
Type
Syntax

Example

Dart Programming - List.length Method

Returns the size of the list.

Syntax


List.length

Example


void main() {
var lst = new List();
lst.add(12);
lst.add(13);
print("The length of the list is : ${lst.length}");
}


The length of the list is : 2

Summary
Type
Syntax

Example`

Dart Programming - List.last Method

Returns the last element in the list.

Syntax

`
List.last
`

Example

`
void main() {
var lst = new List();
lst.add(12);
lst.add(13);
print("The last element of the list is: ${lst.last}");
}
`

It will produce the following output −

`
The last element of the list is: 13
`

Summary
Type
Syntax

Example

Dart Programming - List.reversed Method

Returns an iterable object containing the list values in the reverse order.

Syntax

`
List.reversed
`

Example

`
void main() {
var lst = new List();
lst.add(12);
lst.add(13);
print("The list values in reverse order: ${lst.reversed}");
}
`

It will produce the following output −

`
The list values in reverse order: (13, 12)
`

Summary
Type
Syntax

Example

Dart Programming - List.single Method

Checks if the list has only one element and returns it.

Syntax

`
List.single
`

Example

`
void main() {
var lst = new List();
lst.add(12);
print("The list has only one element: ${lst.single}");
}
`

It will produce the following output −

`
The list values in reverse order: (13, 12)
`

It will produce the following output −

`
The list has only one element: 12
`

This property throws an exception if the List has more than one element in it. The following example illustrates the same −

`
void main() {
var lst = new List();
lst.add(12);
lst.add(10);
print(lst.single);
}
`

If the list has more than one element, then the same code will throw the following exception −

`
Unhandled exception:
Bad state: Too many elements
`

List.single (dart:core-patch/growable_array.dart:234)
main (file:///D:/Demos/Boolean.dart:6:13)
_startIsolate. (dart:isolatepatch/isolate_patch.dart:261)
_RawReceivePortImpl._handleMessage (dart:isolatepatch/isolate_patch.dart:148)

Summary
Type
Syntax
Example
**

Full Summary

  • List Type Sr.No Methods & Description 1 first Returns the first element in the list.

2 isEmpty
Returns true if the collection has no elements.

3 isNotEmpty
Returns true if the collection has at least one element.

4 length
Returns the size of the list.

5 last
Returns the last element in the list.

6 reversed
Returns an iterable object containing the lists values in the reverse order.

7 Single
Checks if the list has only one element and returns it.

Refrenec
dart_programming_list_first_method
dart_programming_list_isempty_method
dart_programming_list_isnotempty_method
dart_programming_list_length_method
dart_programming_list_last_method
dart_programming_list_reversed_method
dart_programming_list_single_method
dart-list

Top comments (0)