Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

dart
set
communitycreator

What is Set last 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.

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

If there are duplicates, the set only keeps the first instance. For example, {'Tom','Alsvin','Eddie','Tom'} results in {'Tom','Alsvin','Eddie'}

The set.last function in Dart is used to get the reference of the last element in the set.

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

Figure 1 shows the visual representation of the set.last function:

Figure 1: Visual representation of set.last function

Syntax

element set_name.last
// where the set_name is the name of the set.

Parameter

This function does not require a parameter.

Return value

The function set.last returns the reference to the set’s last element.

  • It does not remove that element from the set.
  • If the set is empty, it throws a StateError.

Code

The following code shows how to use set.last 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 elements: ${set_1}");
print("set_1 last element: ${set_1.last}");
Set<String> set_2 = {'Tom','Alsvin','Eddie','Tom'};
//set containing non-unique value
print("set_2 elements: ${set_2}");
print("set_2 last element: ${set_2.last}");
}

RELATED TAGS

dart
set
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