How to find the length of a list in Dart

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

Avatar

A web geek, an industry experienced web developer & tutor/instructor residing in India 🇮🇳

Next
Previous

Related