fill method

List<T?> fill(
  1. int length, [
  2. T? value
])

If the number of elements in List is less than length, it is filled with value up to the length elements.

If value is not specified, null is inserted.

Listの要素の数がlength未満の場合、lengthの要素までvalueで埋め尽くします。

valueが指定されない場合はNullが挿入されます。

final array = [1, 2, 3];
final filled = array.fill(5, 0); // [1, 2, 3, 0, 0]

Implementation

List<T?> fill(int length, [T? value]) {
  final tmp = <T?>[];
  for (int i = 0; i < max(length, this.length); i++) {
    if (i < this.length) {
      tmp.add(this[i]);
    } else {
      tmp.add(value);
    }
  }
  return tmp;
}