Dart Runes elementAt()
Syntax & Examples

Runes.elementAt() method

The `elementAt` method in Dart returns the index-th element.


Syntax of Runes.elementAt()

The syntax of Runes.elementAt() method is:

 int elementAt(int index) 

This elementAt() method of Runes returns the indexth element.

Parameters

ParameterOptional/RequiredDescription
indexrequiredThe index of the element to retrieve.

Return Type

Runes.elementAt() returns value of type int.



✐ Examples

1 Accessing an element by index from a list of numbers

In this example,

  1. We create a list numbers containing integers.
  2. We use the elementAt() method with index 1 to retrieve the second element.
  3. We then print the retrieved element to standard output.

Dart Program

void main() {
  List<int> numbers = [1, 2, 3];
  int element = numbers.elementAt(1);
  print(element);
}

Output

2

2 Accessing an element by index from a list of characters

In this example,

  1. We create a list characters containing characters.
  2. We use the elementAt() method with index 2 to retrieve the third element.
  3. We then print the retrieved element to standard output.

Dart Program

void main() {
  List<String> characters = ['a', 'b', 'c'];
  String element = characters.elementAt(2);
  print(element);
}

Output

c

3 Accessing an element by index from a list of strings

In this example,

  1. We create a list words containing strings.
  2. We use the elementAt() method with index 0 to retrieve the first element.
  3. We then print the retrieved element to standard output.

Dart Program

void main() {
  List<String> words = ['hello', 'world'];
  String element = words.elementAt(0);
  print(element);
}

Output

hello

Summary

In this Dart tutorial, we learned about elementAt() method of Runes: the syntax and few working examples with output and detailed explanation for each example.