Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

dart
communitycreator

What is the Set.isNotEmpty method in Dart?

Harris Amjad

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The set.isNotEmpty function in Dart is used to check if a set is empty or not. set.isNotEmpty returns true if a set is not empty; otherwise, it returns false.

The image below shows the visual representation of the set.isNotEmpty function.

Figure 1

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

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


Syntax

set_name.isNotEmpty

set_name is the name of the set.

Parameter

The set.isNotEmpty function does not require any parameters.

Return value

If the set is not empty, the set.isNotEmpty function returns true; otherwise, it returns false.

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

Code

The code below shows how to use the set.isNotEmpty 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 not empty: ${set_1.isNotEmpty}");
//empty set
Set<String> set_2 = {};
print("set_2 is not empty: ${set_2.isNotEmpty}");
//set containing non-unique value
Set<String> set_3 = {'Tom','Alsvin','Eddie','Tom'};
print("set_3 is not empty: ${set_3.isNotEmpty}");
}

RELATED TAGS

dart
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring