Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

dart
communitycreator

What is the list clear() method in Dart?

Maria Elijah

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 clear() method in Dart removes all the items from a list and reduces its length to zero.

Syntax

list_name.clear();

list_name is the name of the list from which the items are removed.

Parameter

The clear() method takes no parameter.

Return type

This method does not return anything. Hence, its return type is void.

Code

The following code shows us how to use the clear() method in Dart:

void main(){
// Create list name
List name = ['John', 'Hamad', 'Maria', 'Rejoice', 'Elena'];
// Display result
print('The elements in the list: ${name}');
// Returns list length
print('The list length:${name.length}');
// remove items in the list
// using clear()
name.clear();
// Returns list length
print('The list length:${name.length}');
}
List "clear()" method in Dart

Note: If the list in question is a fixed-length list, then the method throws an “Unsupported operation: Cannot clear a fixed-length list” error and retains all the items within the list.

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