List in Dart is a collection of objects. The duplicate entry in a list is also allowed.
This article shows how to find the length of a list in Dart.
The property named length is used to get the number of objects in a list.
The list can be classified as โ
- Fixed Length List
- A list whose length cannot be changed at runtime is known as a fixed-length list.
- Growable List
- A list whose length can change at runtime is known as a growable list.
A list is simply an ordered and mutable group of objects.
Starting index of the list is 0(zero) through length - 1.
The length property
The list in Dart provides a property length that returns the total objects within a list.
Syntax
listName.length
The length property gives an integer value i.e the number of objects within the list.
Example
void main()
{
var countries = ["India ๐ฎ๐ณ", "Switzerland ๐จ๐ญ", 1, -9.5, 0, 1];
print(countries);
print('Total elements : ${countries.length}');
}
Output:
[India ๐ฎ๐ณ, Switzerland ๐จ๐ญ, 1, -9.5, 0, 1]
Total elements : 6
The list has 6 elements.
Another way to get the length of a list is by using looping structure but the more preferable way is to use the length property of a list.
Conclusion
To find the length of a list in Dart, use the length property of a list.
Hope you like this!
Keep helping and happy ๐ coding
Related Articles
Deepen your understanding with these curated continuations.
How to Get Your Dart Version from the Command Line
Learn the quick way to check your Dart SDK version on Windows, macOS, or Linux using the `dart --version` command.
Introduction to Flutter: The UI Revolution
Learn why Flutter is the top toolkit for building native mobile, web, and desktop apps from one codebase. Explore Dart, Hot Reload, and Flutter architecture.
Dart - Comments
Improve your code readability with this guide to Dart comments. Learn single-line, multi-line, and doc comments for better Flutter development maintenance.