What is the set.isEmpty method in Dart?

The set.isEmpty function in Dart is used to check if a set is empty. The set.isEmpty function returns true if the set is empty. Otherwise, it returns false.

The illustration below shows the visual representation of the set.isEmpty function.

Visual representation of the set.isEmpty function

In Dart, a set is a particular instance in a list in which all of the inputs are unique.

dart:core is required in order to use this function.


Syntax


bool set_name.isEmpty

set_name is the name of the set.


Parameters

The set.isEmpty function does not require any parameters.

Return value

If the set is empty, the set.isEmpty function returns true. Otherwise, it returns false.


If there are duplicates, then the set will only keep the first instance, i.e., {'Tom','Alsvin','Eddie','Tom'} will result in {'Tom','Alsvin','Eddie'}.

Code

The code below shows how to use the set.isEmpty function in Dart.

import 'dart:convert';
import 'dart:core';
void main() {
Set<String> set_1 = {'Tom','Alsvin','Eddie'};
//set containing unique value
print("set_1 is empty: ${set_1.isEmpty}");
//empty set
Set<String> set_2 = {};
print("set_2 is empty: ${set_2.isEmpty}");
//set containing non-unique value
Set<String> set_3 = {'Tom','Alsvin','Eddie','Tom'};
print("set_3 is empty: ${set_3.isEmpty}");
}

Free Resources