toBoolList method

List<bool> toBoolList({
  1. int trueAtIndex = 0,
})

Converts the current List to a List

The trueAtIndex parameter specifies the index at which the value true should be set in the resulting list. By default, the value true is set at index 0 if trueAtIndex is out of range.

Returns a new List

Example:

List<int> numbers = [0, 1, 2, 3, 4];
List<bool> boolList = numbers.toBoolList(trueAtIndex: 2);
print(boolList); // Output: [false, false, true, false, false]

Implementation

List<bool> toBoolList({int trueAtIndex = 0}) {
  var list = List<bool>.filled(this.length, false, growable: true);
  if (list.asMap().containsKey(trueAtIndex) == true) {
    list[trueAtIndex] = true;
  } else {
    list[0] = true;
  }

  return list;
}