기술은 나의 도구

Dart List Method 사용법

add(E value) → void

  • Adds value to the end of this list, extending the length by one.
List<int> numbers = [1];
numbers.add(2);
print(numbers); // dict : [1, 2]

addAll(Iterable iterable) → void

  • Appends all objects of iterable to the end of this list.
List<int> numbers = [1, 2];
numbers.addAll([3, 4]);
print(numbers); // dict : [1, 2, 3, 4]

any(bool test(E element)) → bool

  • Checks whether any element of this iterable satisfies test.
  • 하나라도 만족하면 true
  • 하나도 만족하지 못하면 false
List<int> numbers = [1, 2, 3, 4];
print(numbers.any((el) => el > 3)); // true
print(numbers.any((el) => el > 4)); // false

asMap() → Map<int, E>

  • An unmodifiable Map view of this list.
  • List 의 인덱스를 key 로 하고 객체를 값으로 하는 Map 으로 변환
List<String> terms = ['tweet', 'fat', 'thin'];
Map<int, String> newTerms = terms.asMap();
print(newTerms); // {0: tweet, 1: fat, 2: thin}

cast() → List

  • Returns a view of this list as a list of R instances.
  • 목록의 요소를 특정 유형으로 변환
  • 상위 유형의 목록이 있고 더 구체적인 유형의 목록이 필요할 때 사용
List<Object> dict = ["tweet", "fat", "thin"];
List<String> newDict = dict.cast<String>();
print(newDict); // ["tweet", "fat", "thin"]

clear() → void

  • Removes all objects from this list; the length of the list becomes zero.
List<int> numbers = [1, 2, 3, 4];
numbers.clear();
print(numbers); // []

contains(Object? element) → bool

  • Whether the collection contains an element equal to element.
List<int> numbers = [1, 2, 3, 4];
print(numbers.contains(1)); // true
print(numbers.contains(5)); // false

elementAt(int index) → E

  • Returns the indexth element.
List<int> numbers = [1, 2, 3, 4];
print(numbers.elementAt(2)); // 3

every(bool test(E element)) → bool

  • Checks whether every element of this iterable satisfies test.
  • 모두 만족하면 true
  • 하나라도 만족하지 못하면 false
List<int> numbers = [1, 2, 3, 4];
print(numbers.every((el) => el > 0)); // true
print(numbers.every((el) => el > 1)); // false

expand(Iterable toElements(E element)) → Iterable

  • Expands each element of this Iterable into zero or more elements.
  • 일반적으로 목록의 각 요소를 반복하고 각 요소에서 다른 유형의 이터러블을 생성하는 방법
List<Map<String, String>> listOfMaps = [
  {'key1': 'value1', 'key2': 'value2'},
  {'key3': 'value3', 'key4': 'value4'},
];
Iterable<String> allValues = listOfMaps.expand((map) => map.values);
print(allValues.toList()); // [value1, value2, value3, value4]

Iterable<int> count(int n) sync* {
	for (var i = 1; i <= n; i++) {
		yield i;
	}
}
var numbers = [1, 3, 0, 2];
print(numbers.expand(count).toList()); // [1, 1, 2, 3, 1, 2]

fillRange(int start, int end, [E? fillValue]) → void

  • Overwrites a range of elements with fillValue.
List<int> numbers = List.filled(5, 1); // numbers: [1, 1, 1, 1, 1]
numbers.fillRange(2, 4, 6);
print(numbers); // [1, 1, 6, 6, 1]

firstWhere(bool test(E element), {E orElse()?}) → E

  • The first element that satisfies the given predicate test.
List<int> numbers = [3, 5, 6, 4];
int overThree = numbers.firstWhere((el) => el > 3);
print(overThree); // 5

fold(T initialValue, T combine(T previousValue, E element)) → T

  • Reduces a collection to a single value by iteratively combining each element of the collection with an existing value
List<int> numbers = [1, 2, 3, 4];
final result = numbers.fold(0, (prev, el) => prev + el);
print(result); // 10

followedBy(Iterable other) → Iterable

  • Creates the lazy concatenation of this iterable and other.
List<int> numbers = [1, 2, 3, 4];
final result = numbers.followedBy([5, 6]);
print(result.toList()); // [1, 2, 3, 4, 5, 6]

forEach(void action(E element)) → void

  • Invokes action on each element of this iterable in iteration order.
List<int> numbers = [1, 2, 3, 4];

// case 1
numbers.forEach((el) => print(el + 1));

// case 2
numbers.forEach((el) {
  var result = el + 1;
  print(result);
});

getRange(int start, int end) → Iterable

  • Creates an Iterable that iterates over a range of elements.
List<int> numbers = [1, 2, 3, 4];
final ranged = numbers.getRange(0, 3);
print(ranged.toList()); // [1, 2, 3]

indexOf(E element, [int start = 0]) → int

  • The first index of element in this list.
List<int> numbers = [1, 2, 3, 4, 3, 2, 1];
print(numbers.indexOf(3)); // 2

indexWhere(bool test(E element), [int start = 0]) → int

  • The first index in the list that satisfies the provided test.
List<int> numbers = [1, 2, 3, 4, 3, 2, 1];
print(numbers.indexWhere((el) => el > 2)); // 2

insert(int index, E element) → void

  • Inserts element at position index in this list.
List<int> numbers = [1, 2, 3, 4];
numbers.insert(2, 5);
print(numbers); // [1, 2, 5, 3, 4]

insertAll(int index, Iterable iterable) → void

  • Inserts all objects of iterable at position index in this list.
List<int> numbers = [1, 2, 3, 4];
numbers.insertAll(2, [5, 6]);
print(numbers); // [1, 2, 5, 6, 3, 4]

join([String separator = ""]) → String

  • Converts each element to a String and concatenates the strings.
List<String> texts = ["my","name","is","moz"];
print(texts.join(" ")); // my name is moz

lastIndexOf(E element, [int? start]) → int

  • The last index of element in this list.
List<int> numbers = [1, 2, 3, 4, 3, 2, 1];
print(numbers.lastIndexOf(2)); // 5

lastIndexWhere(bool test(E element), [int? start]) → int

  • The last index of element in this list.
List<int> numbers = [1, 2, 3, 4, 3, 2, 1];
print(numbers.lastIndexWhere((el) => el > 2)); // 4

lastWhere(bool test(E element), {E orElse()?}) → E

  • The last element that satisfies the given predicate test.
List<int> numbers = [5, 6, 7, 4, 3, 2, 1];
print(numbers.lastWhere((el) => el > 2)); // 3

map(T toElement(E e)) → Iterable

  • The current elements of this iterable modified by toElement.
List<int> numbers = [1, 2, 3, 4];
print(numbers.map((el) => el + 2).toList()); // [3, 4, 5, 6]

noSuchMethod(Invocation invocation) → dynamic

  • Invoked when a nonexistent method or property is accessed.
  • 존재하지 않는 메서드 또는 인스턴스 변수를 사용하려고 시도할 때 사용자 지정 동작을 제공하기 위해 재정의할 수 있는 특수 함수
class MyClass {
  
  noSuchMethod(Invocation invocation) {
    print('You tried to use a non-existent member: ' +
        '${invocation.memberName}');
  }
}

reduce(E combine(E value, E element)) → E

  • Reduces a collection to a single value by iteratively combining elements of the collection using the provided function.
List<int> numbers = [1, 2, 3, 4];
final result = numbers.reduce((value, element) => value + element);
print(result); // 10

remove(Object? value) → bool

  • Removes the first occurrence of value from this list.
List<int> numbers = [1, 2, 3, 4, 3, 2, 1];
numbers.remove(1);
print(numbers);

removeAt(int index) → E

  • Removes the object at position index from this list.
List<int> numbers = [1, 2, 3, 4, 3, 2, 1];
final removed = numbers.removeAt(5);
print(removed); // 2
print(numbers); // [1, 2, 3, 4, 3, 1]

removeLast() → E

  • Removes and returns the last object in this list.
List<int> numbers = [1, 2, 3, 4];
final removed = numbers.removeLast();
print(removed); // 4
print(numbers); // [1, 2, 3]

removeRange(int start, int end) → void

  • Removes a range of elements from the list.
List<int> numbers = [1, 2, 3, 4, 5, 6, 7];
numbers.removeRange(2, 4);
print(numbers);

removeWhere(bool test(E element)) → void

  • Removes all objects from this list that satisfy test.
List<int> numbers = [1, 2, 3, 4, 5, 6, 7];
numbers.removeWhere((el) => el < 4);
print(numbers); // [4, 5, 6, 7]

replaceRange(int start, int end, Iterable replacements) → void

  • Replaces a range of elements with the elements of replacements.
List<int> numbers = [1, 2, 3, 4, 5, 6, 7];
numbers.replaceRange(3, 5, [8, 9, 10]);
print(numbers); // [1, 2, 3, 8, 9, 10, 6, 7]

retainWhere(bool test(E element)) → void

  • Removes all objects from this list that fail to satisfy test.
List<int> numbers = [1, 2, 3, 4, 5, 6, 7];
numbers.retainWhere((el) => el % 2 == 0);
print(numbers); // [2, 4, 6]

setAll(int index, Iterable iterable) → void

  • Overwrites elements with the objects of iterable.
List<int> numbers = [1, 2, 3, 4];
numbers.setAll(1, [5, 6]);
print(numbers); // [1, 5, 6, 4]

setRange(int start, int end, Iterable iterable, [int skipCount = 0]) → void

  • Writes some elements of iterable into a range of this list.
List<int> numbers = [1, 2, 3, 4];
numbers.setRange(1, 4, [5, 6, 7]);
print(numbers); // [1, 5, 6, 7]

shuffle([Random? random]) → void

  • Shuffles the elements of this list randomly.
List<int> numbers = [1, 2, 3, 4];
numbers.shuffle(); // ex. [2, 4, 3, 1]

singleWhere(bool test(E element), {E orElse()?}) → E

  • The single element that satisfies test.
List<int> numbers = [1, 2, 3, 4];
// final result = numbers.singleWhere((el) => el > 2); // Error (too many elements)
final result = numbers.singleWhere((el) => el < 2); // OK
print(result); // 1

skip(int count) → Iterable

  • Creates an Iterable that provides all but the first count elements.
List<int> numbers = [1, 2, 3, 4];
final result = numbers.skip(2);
print(result.toList()); // [3, 4]

skipWhile(bool test(E value)) → Iterable

  • Creates an Iterable that skips leading elements while test is satisfied.
List<int> numbers = [1, 2, 3, 4];
final result = numbers.skipWhile((el) => el.isOdd);
print(result.toList()); // [2, 3, 4]

sort([int compare(E a, E b)?]) → void

  • Sorts this list according to the order specified by the compare function.
List<int> numbers = [1, 2, 3, 4];
numbers.sort((a, b) => b.compareTo(a));
print(numbers); // [4, 3, 2, 1]

sublist(int start, [int? end]) → List

  • Returns a new list containing the elements between start and end.
List<int> numbers = [1, 2, 3, 4];
print(numbers.sublist(2, 4)); // [3, 4]

take(int count) → Iterable

  • Creates a lazy iterable of the count first elements of this iterable.
List<int> numbers = [1, 2, 3, 4];
print(numbers.take(3).toList()); // [1, 2, 3]

takeWhile(bool test(E value)) → Iterable

  • Creates a lazy iterable of the leading elements satisfying test.
List<int> numbers = [1, 2, 3, 4];
print(numbers.takeWhile((el) => el.isOdd).toList()); // [1]

toList({bool growable = true}) → List

  • Creates a List containing the elements of this Iterable.
Iterable<int> numbers = [1, 2, 3, 4];
print(numbers.toList()); // [1, 2, 3, 4]

toSet() → Set

  • Creates a Set containing the same elements as this iterable.
List<int> numbers = [1, 2, 3, 4, 3, 1];
print(numbers.toSet()); // {1, 2, 3, 4}

toString() → String

  • A string representation of this object.
List<int> numbers = [1, 2, 3, 4, 3, 1];
  print(numbers.toString()); // "[1, 2, 3, 4, 3, 1]"

where(bool test(E element)) → Iterable

  • Creates a new lazy Iterable with all elements that satisfy the predicate test.
List<int> numbers = [1, 2, 3, 4, 3, 1];
print(numbers.where((el) => el.isEven).toList()); // [2, 4]

whereType() → Iterable

  • Creates a new lazy Iterable with all elements that have type T.
List<Object> numbers = [1, 2, 'fat', 4, 'thin', 1];
print(numbers.whereType<int>().toList()); // [1, 2, 4, 1]
  • #Dart
  • #List
  • #Method

Table of Contents