Dart List length
Syntax & Examples


Syntax of List.length

The syntax of List.length property is:

 

This length property of List the number of objects in this list.

Return Type

List.length returns value of type .



✐ Examples

1 Get length of a list

In this example,

  1. We create a list numbers containing integers.
  2. We then use the length property to get the length of the list.
  3. We print the length to standard output.

Dart Program

void main() {
  List<int> numbers = [1, 2, 3, 4, 5];
  int listLength = numbers.length;
  print('Length of numbers list: $listLength');
}

Output

Length of numbers list: 5

2 Get length of a set

In this example,

  1. We create a set characters containing strings.
  2. We then use the length property to get the length of the set.
  3. We print the length to standard output.

Dart Program

void main() {
  Set<String> characters = {'a', 'b', 'c'};
  int setLength = characters.length;
  print('Length of characters set: $setLength');
}

Output

Length of characters set: 3

3 Get length of a string

In this example,

  1. We create a string str with a value.
  2. We then use the length property to get the length of the string.
  3. We print the length to standard output.

Dart Program

void main() {
  String str = 'Hello, world!';
  int strLength = str.length;
  print('Length of string: $strLength');
}

Output

Length of string: 13

Summary

In this Dart tutorial, we learned about length property of List: the syntax and few working examples with output and detailed explanation for each example.