Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn List in Dart | Variables and Data Types in Dart
Introduction to Dart

bookList in Dart

Note
Definition

A List in Dart is a built-in data type that represents an ordered collection of elements. Each element in a List usually has the same data type, such as integers, floating-point numbers, strings, or other objects.

Lists are useful for:

  • Storing data about a group of related items, such as a shopping list, to-do list, or contact list.
  • Tracking sequences of events, such as a history log or a list of steps needed to complete a task.

They help keep related information organized and easy to access.

Syntax

Let's look at the scheme of creating a List:

List<elements_type> list_name = [element_1, element_2];
  • List: command to create a list;
  • <elements_type>: the data type for values will be stored in the middle of the list;
  • list_name: list name;
  • [element_1, element_2]: commas (,) separate the list elements.
main.dart

main.dart

copy
1
List<int> numbers = [1, 2, 3, 4, 5];
  • List of integer numbers;
  • <int> type elements from List.

Adding Items

Let's add a value to the already existing list of numbers.

main.dart

main.dart

copy
123456
void main() { List<int> numbers = [111, 22, 32]; print(numbers); // Output [111, 22, 32] numbers.add(6); // Adding an item to a list print(numbers); // Output [111, 22, 32, 6] }
numbers.add(6);

The expression numbers.add(6); calls the add() method on the variable numbers, adding the value 6 to it.

Deleting Items

list_name.remove(element)

The expression list_name.remove(element) calls the remove() method on the variable, deleting the first occurrence of the specified value.

main.dart

main.dart

copy
123456
void main() { List<int> numbers = [11, 22, 23]; print(numbers); // [11, 22, 23] numbers.remove(22); print(numbers); // [11, 23] }
question mark

Which method should you use to add a new element to the end of a List in Dart?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 6

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Awesome!

Completion rate improved to 4.55

bookList in Dart

Swipe to show menu

Note
Definition

A List in Dart is a built-in data type that represents an ordered collection of elements. Each element in a List usually has the same data type, such as integers, floating-point numbers, strings, or other objects.

Lists are useful for:

  • Storing data about a group of related items, such as a shopping list, to-do list, or contact list.
  • Tracking sequences of events, such as a history log or a list of steps needed to complete a task.

They help keep related information organized and easy to access.

Syntax

Let's look at the scheme of creating a List:

List<elements_type> list_name = [element_1, element_2];
  • List: command to create a list;
  • <elements_type>: the data type for values will be stored in the middle of the list;
  • list_name: list name;
  • [element_1, element_2]: commas (,) separate the list elements.
main.dart

main.dart

copy
1
List<int> numbers = [1, 2, 3, 4, 5];
  • List of integer numbers;
  • <int> type elements from List.

Adding Items

Let's add a value to the already existing list of numbers.

main.dart

main.dart

copy
123456
void main() { List<int> numbers = [111, 22, 32]; print(numbers); // Output [111, 22, 32] numbers.add(6); // Adding an item to a list print(numbers); // Output [111, 22, 32, 6] }
numbers.add(6);

The expression numbers.add(6); calls the add() method on the variable numbers, adding the value 6 to it.

Deleting Items

list_name.remove(element)

The expression list_name.remove(element) calls the remove() method on the variable, deleting the first occurrence of the specified value.

main.dart

main.dart

copy
123456
void main() { List<int> numbers = [11, 22, 23]; print(numbers); // [11, 22, 23] numbers.remove(22); print(numbers); // [11, 23] }
question mark

Which method should you use to add a new element to the end of a List in Dart?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 6
some-alt