🐡

【Flutter】Listのindexの存在チェック

2023/02/02に公開

asMap().containsKeyをつかう

List<int> numbers = [1, 2, 3, 4, 5,];

//Check if index 7 is valid
if (numbers.asMap().containsKey(7)) {
  print('Exists');
} else {
  print('Doesn\'t exist');
}

//[EDIT] Check if item at index 4 exists and is 5
if (numbers.asMap()[4] == 5) {
  print("Index 4 = 5");
} else {
  print("Index 4 != 5");
}

Ref

https://stackoverflow.com/a/59763391

Discussion