Last active
December 3, 2023 13:52
-
-
Save abdorll/f9e32c5593d592886f44c18faabc3240 to your computer and use it in GitHub Desktop.
List in dart example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void main() { | |
// ==================BASIC USAGE 1. | |
// Creating a list of integers | |
List<int> numbers = [1, 2, 3, 4, 5]; | |
// Creating a list of strings | |
List<String> fruits = ['apple', 'banana', 'cherry']; | |
// Adding elements to a list | |
fruits.add('orange'); | |
// Accessing elements by index | |
String firstFruit = fruits[0]; // 'apple' | |
// Removing an element from a list | |
fruits.remove('banana'); | |
// Finding the length of a list | |
int listLength = fruits.length; // 2 | |
// Iterating through a list | |
for (String fruit in fruits) { | |
print(fruit); | |
} | |
// Lists can contain elements of different types | |
List<dynamic> mixedList = [1, 'apple', true]; | |
// You can create an empty list and add elements later | |
List<double> emptyList = []; | |
emptyList.add(3.14); | |
// ==================LIST PROPERTIES | |
// 1. length → int (Returns the length of the list) | |
int listLengthProperty = fruits.length; | |
// 2. isEmpty → bool (Returns true if the list is empty) | |
bool isListEmpty = fruits.isEmpty; | |
// 3. isNotEmpty → bool (Returns true if the list is not empty) | |
bool isListNotEmpty = fruits.isNotEmpty; | |
// 4. reversed → Iterable<E> (Returns an iterable of the list's elements in reverse order) | |
Iterable<String> reversedFruits = fruits.reversed; | |
// ==================LIST METHODS | |
// 1. add() → void (Adds an element to the end of the list) | |
fruits.add('grape'); | |
// 2. insert() → void (Inserts an element at a specific index) | |
fruits.insert(1, 'kiwi'); | |
// 3. remove() → bool (Removes the first occurrence of an element) | |
bool removed = fruits.remove('apple'); | |
// 4. clear() → void (Removes all elements from the list) | |
fruits.clear(); | |
// 5. indexOf() → int (Returns the index of the first occurrence of an element) | |
int indexOfCherry = fruits.indexOf('cherry'); | |
// 6. lastIndexOf() → int (Returns the index of the last occurrence of an element) | |
int lastIndexOfCherry = fruits.lastIndexOf('cherry'); | |
// 7. shuffle() → void (Randomly shuffles the elements in the list) | |
List<int> numbers = [1, 2, 3, 4, 5]; | |
numbers.shuffle(); | |
// 8. sort() → void (Sorts the elements of the list) | |
List<String> fruits = ['apple', 'banana', 'cherry', 'date']; | |
fruits.sort(); // Sorts in alphabetical order | |
// Custom sorting | |
fruits.sort((a, b) => a.length.compareTo(b.length)); | |
// 9. reduce() → E (Applies a binary function to the elements and reduces the list to a single value) | |
int sum = numbers.reduce((value, element) => value + element); | |
// 10. fold() → E (Combines the elements of the list using a binary function and an initial value) | |
int product = numbers.fold(1, (value, element) => value * element); | |
// 11. followedBy(Iterable<E> other) → Iterable<E> Creates the lazy concatenation of this iterable and other. | |
List<int>numberst2 = numbers.followedBy([17, 9, 20]).toList(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment