Set is one of the data structures in Dart Collections used to store the collection of values or objects, and does not allow duplicate elements.

A Set is an unordered Array of the same type, with unique elements.

The Set is a Class of dart:core packages in Dart language.

A set object is used to store the array of the same element types with unique elements and order is not important.

How do you create a Set object in Dart?

The set object is created using the set keyword var variable = {};

  • Fixed List creation List.filled() method used to create a list.

Syntax

List List.filled(int length, String fill, { bool growable = false, })

It creates a list with a length property and filled the list with the fill value. growable is a boolean value to indicate it is the fixed or growable list.

Here is an example of creating a fixed list in dart

void main() {
  var listvariable = new List<String>.filled(3, "", growable: false);

  listvariable[0] = "one";
  listvariable[1] = "two";
  listvariable[2] = "three";
  print(listvariable);
}

Output:

[one, two, three]

This is equal to an array in Dart.

In the above example, Create a list of length=3 with filled values empty.

each element is updated with an index starting from zero to 2 since the length is three.

It throws an error if you try to insert the fixed length with an index greater than or equal to the length.

Uncaught Error: RangeError (index): Index out of range: index should be less than 3: 4:

void main() {
  var listvariable = new List<String>.filled(3, ", growable: false);

  listvariable[0] = "one";
  listvariable[1] = "two";
  listvariable[2] = "three";
  listvariable[4] = "four";


}

Dart Growable List

Growable list size is not known at declaration and size can be changed at runtime

It can be created in the following ways.

  • use the filled method List.filled method allows you to fill the size of elements with a growable boolean value

Here is an example

void main() {
  var listvariable = new List<String>.filled(0, "", growable: true);

  listvariable = ["one", "two", "three"];
  print(listvariable);
}
  • Using the empty method with growable is true The List.empty() method allows you to create an empty list with a growable size.
void main() {
  List<String> listvariable = List<String>.empty(growable: true); // []

  listvariable = ["one", "two", "three"];
  print(listvariable);
}
  • use empty brackets

List variable is created with empty brackets also called empty list literal. list values are initialized during declaration or empty with list literal syntax.

void main() {
  var listvariable = [];
  var listvariable1 = [1, 2, 3];

  listvariable = ["one", "two", "three"];
  listvariable1.add(4);
  print(listvariable);
  print(listvariable1);
}

The list can be created with the default constructor in my previous versions of dart

We can not use new List(3) or new List() since the default constructor is deprecated.

void main() {
  var listvariable = new List();

  print(listvariable);
}

The above code throws an error Error: Can’t use the default List constructor.

Dart List properties

Following is the list of class properties.

For example, a list contains the following elements.

 var numbers= [1,3,4,5]

|List Property | Description | result| |:——–| ————— | |first| First element in a list| numbers.first is 1 | |last| last element in a list| numbers.last is 5 |

|length| Returns size of an list| numbers.length is 4 | |isEmpty| true - if list is empty,else false| numbers.isEmpty is false | |isNonEmpty| true - if the list is not empty, else false| numbers.isNonEmpty is true | |reversed| List is reversed order| numbers.reversed is [5,4,3,1] | |Single| true if the list contains a single element, else false| numbers.Single is false |