Dart Tutorials

Dart List first
Syntax & Examples

Syntax of List.first

The syntax of List.first property is:

 

This first property of List the first element.

Return Type

List.first returns value of type .



✐ Examples

1 Get the first element from a list of numbers

In this example,

  1. We create a list named numbers with elements [1, 2, 3, 4, 5].
  2. We access the first element of the list using the first property.
  3. The value of the first number is stored in firstNumber.
  4. We print the first number to standard output.

Dart Program

void main() {
  List<int> numbers = [1, 2, 3, 4, 5];
  int firstNumber = numbers.first;
  print('First number: $firstNumber'); // Output: First number: 1
}

Output

First number: 1

2 Get the first element from a list of characters

In this example,

  1. We create a list named characters with elements ['a', 'b', 'c'].
  2. We access the first element of the list using the first property.
  3. The value of the first character is stored in firstCharacter.
  4. We print the first character to standard output.

Dart Program

void main() {
  List<String> characters = ['a', 'b', 'c'];
  String firstCharacter = characters.first;
  print('First character: $firstCharacter'); // Output: First character: a
}

Output

First character: a

3 Get the first element from a list of strings

In this example,

  1. We create a list named strings with elements ['apple', 'banana', 'cherry'].
  2. We access the first element of the list using the first property.
  3. The value of the first string is stored in firstString.
  4. We print the first string to standard output.

Dart Program

void main() {
  List<String> strings = ['apple', 'banana', 'cherry'];
  String firstString = strings.first;
  print('First string: $firstString'); // Output: First string: apple
}

Output

First string: apple

Summary

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