1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Dart 3点リーダー(ドット3つ)の利用法

Last updated at Posted at 2022-08-02

配列の結合によく使われている3点リーダーの役割に関しての記述です。ズバリ、配列の中身を展開してくれる演算子となっています。

var arrayInt = [1, 2, 3];

print(...arrayInt); => // 1 2 3
var arrayString = ['a', 'b', 'c'];

print(...arrayString); => // a b c

展開した配列を結合。

var arrayInt = [1, 2, 3];
var arrayString = ['a', 'b', 'c'];

var addArray = [...arrayInt, ...arrayString];

print(addArray); => // [1, 2, 3, a, b, c]

新しい配列を作成して関数で値を操作。

void main() {
  final list = ['A', 'B', 'C'];
  print(list); // [A, B, C]
  print(add(list, 'D')); // [A, B, C, D]
  print(list); // [A, B, C]
}

add(List list, String v) {  
  final list2 = [...list];
  list2.add(v);
  return list2;  
} 

アロー関数で1行で返すと。

add(List list, String v) => [...list,v];
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?