A list is an ordered collection of items. In Dart, lists are used to store multiple values in a single variable. Lists can be of fixed or variable length, and they support a variety of methods and properties for manipulating the elements.
List.filled(size, fill) to create a list with a fixed size.<Type>[elements] or List<Type> for a dynamic, resizable list.add(element) or addAll([elements]) for growable lists.list[index] to access elements.list[index] = value.remove(element), removeAt(index), or removeLast() for growable lists.for loop or forEach method to iterate over list elements.length, isEmpty, contains(element) are useful properties and methods.void main() { // Creating a fixed-length list var fixedList = List<int>.filled(5, 0); // List of 5 integers, all initialized to 0 print(fixedList); // Output: [0, 0, 0, 0, 0]
// Creating a growable list var growableList = <String>['Alice', 'Bob', 'Charlie']; print(growableList); // Output: [Alice, Bob, Charlie]
// Adding elements to a growable list growableList.add('Diana'); print(growableList); // Output: [Alice, Bob, Charlie, Diana]
// Accessing list elements print(growableList[1]); // Output: Bob
// Changing list elements growableList[1] = 'Bill'; print(growableList); // Output: [Alice, Bill, Charlie, Diana]
// Removing elements from a growable list growableList.remove('Charlie'); print(growableList); // Output: [Alice, Bill, Diana]
// Iterating over a list for (var name in growableList) { print(name); } // Output: // Alice // Bill // Diana
// List properties and methods print(growableList.length); // Output: 3 print(growableList.isEmpty); // Output: false print(growableList.contains('Alice')); // Output: true
// Combining lists var moreNames = ['Eve', 'Frank']; var allNames = growableList + moreNames; print(allNames); // Output: [Alice, Bill, Diana, Eve, Frank] }