fill method
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;
}