List in Flutter Dart


A very commonly used collection in programming is an array. Dart represents arrays in the form of List objects. A List is simply an ordered group of objects. The dart:core library provides the List class that enables creation and manipulation of lists.

The logical representation of a list in Dart is given below −

  • test_list − is the identifier that references the collection.

  • The list contains in it the values 12, 13, and 14. The memory blocks holding these values are known as elements.

  • Each element in the List is identified by a unique number called the index. The index starts from zero and extends up to n-1 where n is the total number of elements in the List. The index is also referred to as the subscript.

Lists can be classified as −

  • Fixed Length List
  • Growable List

Let us now discuss these two types of lists in detail.

Fixed Length List

A fixed length list’s length cannot change at runtime. The syntax for creating a fixed length list is as given below −

Step 1 − Declaring a list

The syntax for declaring a fixed length list is given below −

var list_name = new List[initial_size]

The above syntax creates a list of the specified size. The list cannot grow or shrink at runtime. Any attempt to resize the list will result in an exception.

Step 2 − Initializing a list

The syntax for initializing a list is as given below −

lst_name[index] = value;

Example

Live Demo void main[] { var lst = new List[3]; lst[0] = 12; lst[1] = 13; lst[2] = 11; print[lst]; }

It will produce the following output

[12, 13, 11]

Growable List

A growable list’s length can change at run-time. The syntax for declaring and initializing a growable list is as given below −

Step 1 − Declaring a List

var list_name = [val1,val2,val3] --- creates a list containing the specified values OR var list_name = new List[] --- creates a list of size zero

Step 2 − Initializing a List

The index / subscript is used to reference the element that should be populated with a value. The syntax for initializing a list is as given below −

list_name[index] = value;

Example

The following example shows how to create a list of 3 elements.

Live Demo void main[] { var num_list = [1,2,3]; print[num_list]; }

It will produce the following output

[1, 2, 3]

Example

The following example creates a zero-length list using the empty List[] constructor. The add[] function in the List class is used to dynamically add elements to the list.

Live Demo void main[] { var lst = new List[]; lst.add[12]; lst.add[13]; print[lst]; }

It will produce the following output

[12, 13]

List Properties

The following table lists some commonly used properties of the List class in the dart:core library.

Sr.No Methods & Description
1 first

Returns the first element in the list.

2 isEmpty

Returns true if the collection has no elements.

3 isNotEmpty

Returns true if the collection has at least one element.

4 length

Returns the size of the list.

5 last

Returns the last element in the list.

6 reversed

Returns an iterable object containing the lists values in the reverse order.

7 Single

Checks if the list has only one element and returns it.

class Sale { int employeeId; double price; Sale[this.employeeId, this.price]; } class Employee { int id; List sales; Employee [this.id, this.sales]; } void main[] { //Create a list of employees and their respective sales List employees = new List[]; employees.add[new Employee[1, [new Sale[1, 100.50], new Sale[1, 300.25]]]]; employees.add[new Employee[2, [new Sale[2, 300.00], new Sale[2, 50.25], new Sale[2, 150.00]]]]; employees.add[new Employee[3, [new Sale[2, 400.00], new Sale[2, 30.75], new Sale[3, 50.00]]]]; //Sort so that the employee with the most sales is on top and so on... employees.sort[[a, b] => [b.sales.fold[0, [prev, element] => prev + element.price]].compareTo[a.sales.fold[0, [prev, element] => prev + element.price]]]; log[employees]; //prints Employee #2, followed by Employee #3, then ending with Employee #1 } void log[var lst] { lst.forEach[[l] => print["Employee #${l.id} has ${l.sales.length} sales totaling ${l.sales.fold[0, [prev, element] => prev + element.price]} dollars!"]]; }

Dart supports many different types of collections, or data types that contain multiple values. The most common collections:

  • List [known as an array in some languages]
  • Set
  • Map

Lists

Perhaps the most common collection in nearly every programming language is the array, or ordered group of objects. In Dart, arrays are List objects, so most people just call them lists.

Dart list literals look like JavaScript array literals. Here's a simple Dart list:

var list = List[]; list.add[1, 2]; // list literal definition var list2 = [1, 2, 3];

Dart infers that this is variable of type List. When using data structures like a List or Map, you use < and > to define the types of the values within the List or Map.

The above could also be written like this:

final List list = [1, 2, 3];

Because Dart is type safe, every member in a collection must be the same type. If you really wanted a list with different types, you could define the list as List, but this is rarely useful.

The List class includes a number of members to manipulate a list.

final list = [1,2,3]; assert[list.length == 3]; // true print[list.first]; // 1 print[list.last]; // 3 list.add[4]; list.addAll[5,6]; print[list]; // [1,2,3,4,5,6]

Spread operator

Lists support being "spread out" into other lists. The spread operator puts each element from a list into a target list.

var list = [1,2,3]; var list2 = [0, ...list]; print[list2]; // [0,1,2,3]; // use null aware spread to avoid null-pointer crashes var list3 = [0, ...?list2];

Collection If

You can use simple if and for statements inside list literals in order to programmatically add values. You cannot include a code block with these if and for statements inside collections, you can only add single values.

bool showToday = false; var listOfDays = [ "Yesterday", if [showToday] "Today", // won't be added! "Tomorrow", ]; print[listOfDays]; // ["Yesterday", "Tomorrow"]

Collection For

Collection for is used to add multiple values to a list programmatically.

var listOfDays = [ DateTime[2018, 5, 6], DateTime[2016, 5, 7], DateTime[2018, 5, 8], ]; var humanReadableListOfDays = [ "2018-05-05 00:00:00.000", for [var day in listOfDays] day.toString[], "2018-05-09 00:00:00.000", ]; print[humanReadableListOfDays]; // ["2018-05-05 00:00:00.000", "2018-05-06 00:00:00.000", "2018-05-07 00:00:00.000", "2018-05-08 00:00:00.000", "2018-05-09 00:00:00.000",]

Video liên quan

Chủ Đề