Dart List length
Syntax & Examples
Syntax of List.length
The syntax of List.length property is:
This length property of List the number of objects in this list.
Return Type
List.length returns value of type .
✐ Examples
1 Get length of a list
In this example,
- We create a list
numbers
containing integers. - We then use the
length
property to get the length of the list. - We print the length to standard output.
Dart Program
void main() {
List<int> numbers = [1, 2, 3, 4, 5];
int listLength = numbers.length;
print('Length of numbers list: $listLength');
}
Output
Length of numbers list: 5
2 Get length of a set
In this example,
- We create a set
characters
containing strings. - We then use the
length
property to get the length of the set. - We print the length to standard output.
Dart Program
void main() {
Set<String> characters = {'a', 'b', 'c'};
int setLength = characters.length;
print('Length of characters set: $setLength');
}
Output
Length of characters set: 3
3 Get length of a string
In this example,
- We create a string
str
with a value. - We then use the
length
property to get the length of the string. - We print the length to standard output.
Dart Program
void main() {
String str = 'Hello, world!';
int strLength = str.length;
print('Length of string: $strLength');
}
Output
Length of string: 13
Summary
In this Dart tutorial, we learned about length property of List: the syntax and few working examples with output and detailed explanation for each example.