• Guides
    • Getting started with Dart
  • Blog
  • FAQ
    • Dart
    • Flutter
  • Contact
  • Guides
    • Getting started with Dart
  • Blog
  • FAQ
    • Dart
    • Flutter
  • Contact
  • Getting Started with Dart
    • Dart Variables
    • Dart Operators
    • Dart Comments
  • Dart Data Types
    • Dart Strings
    • Dart Numbers
    • Dart Booleans
    • Dart Lists
    • Dart Maps
  • Control Flow
    • Dart if...else Statements
    • Dart switch Statement
    • Dart for Loop
    • Dart for...in Loop
    • Dart while and do...while Loops
    • break and continue Statements
  • Dart Functions
  • Dart Classes
  • Dart Objects
  • Dart Constructors
  • Dart Exceptions

Dart Lists

157 views 0

Written by Ajaya Shrestha
January 3, 2022

In this chapter, we will learn about dart lists data types and various properties and methods available within lists with the help of examples.


Dart Lists

The lists are collections of ordered groups of objects. The lists are also known as arrays.

We can create a list data type by enclosing the values in square braces. For example,

var numbers = [4, 8, 5];

Another way to create a list is by passing the new keyword to the List object. For example,

var names = new List();
names[0] = 4; 
names[1] = 8; 
names[2] = 5;

In dart lists, all the values should have the same data type.


Accessing List Values

The list values are also known as elements.

Each element has it’s index number which indicates the position in the list.

The element’s index starts with 0. And the last element’s index will be length-1, where length indicates the number of elements in the list.

The index number can be used to access the element from the list. For example,

void main() {
var numbers = [4, 8, 5];

print(numbers[0]);
print(numbers[1]);
print(numbers[2]);

}

Output

4
8
5

Updating List Values

We can update the list values by accessing the index of the list and adding the new values. For example,

void main() {
var numbers = [4, 8, 5];
print(numbers);
numbers[2] = 22;
print(numbers);

}

Output

[4, 8, 5]
[4, 8, 22]

Dart List Properties

Dart list also provides very useful properties. Some of them are:

PropertiesDescription
isEmptyReturns true if the list has no elements
isNotEmptyReturns true if the list has at least one element
lengthReturns the number of elements in the list
firstReturns the first element in the list
lastReturns the last element in the list
reversedReturns an iterable object containing the lists values in the reverse order
singleChecks if the list has only one element and returns it

Dart List Properties Examples

void main() {

var names = ["John", "Jackson", "Anna", "Amanda"];
var names1 = ["Amanda"];

print(names.isEmpty);
print(names.isNotEmpty);
print(names.length);
print(names.first);
print(names.last);

print(names.reversed);
  
print(names1.single);
}

Output

false
true
4
John
Amanda
(Amanda, Anna, Jackson, John)
Amanda

Dart List Methods

Dart list also provides various useful methods. Some of them are:

MethodsDescription
add()Adds the specified value at the end of the list
addAll()Adds multiple values to the given list
insert()Adds the specified value at the specified index of the list
remove()Removes one element at a time from the given list.
removeAt()Removes an element from the specified index position
removeLast()Remove the last element from the given list
removeRange()Removes the item within the specified range

Dart List Methods Examples

void main() {
var numbers = [4, 8, 5];
print(numbers);
numbers.add(12);
print(numbers);

numbers.addAll([15, 16, 18]);
print(numbers);

numbers.insert(2, 55);
print(numbers);

numbers.remove(8);
print(numbers);

numbers.removeAt(3) ;  
print(numbers);

numbers.removeLast();
print(numbers);

numbers.removeRange(2,5);
print(numbers);

}

Output

[4, 8, 5]
[4, 8, 5, 12]
[4, 8, 5, 12, 15, 16, 18]
[4, 8, 55, 5, 12, 15, 16, 18]
[4, 55, 5, 12, 15, 16, 18]
[4, 55, 5, 15, 16, 18]
[4, 55, 5, 15, 16]
[4, 55]

In the next chapter, we will learn about dart maps.

Was this helpful?

Yes  No
Related Articles
  • Dart Constructors
  • Dart Exceptions
  • Dart Objects
  • Dart Classes
  • Control Flow
  • Dart Functions
Previously
Dart Booleans
Up Next
Dart Maps
flutter-dev-experts




FLUTTERGUIDE
FlutterGuide is an easy learning guide platform to help you build apps for any screen. The leading Dart/Flutter guides for getting started and become pro.
Guides
  • Getting started with Dart 22
  • Private Documentation 1
Categories
  • Firebase
  • Dart
  • iOS
  • API
  • Bloc
  • Flutter
  • Widgets
  • Mobile
  • State Management
  • Design
  • Animations
  • Flutter Designs
About
About Us
Guides
Blogs
FAQs
Contact
  • Privacy Policy
  • Terms & Conditions
  • Copyright 2022 Fawesome Apps. All Rights Reserved