isListOfListValuesAllEquals function

bool isListOfListValuesAllEquals(
  1. Iterable<List>? list, {
  2. Object? eqValue,
  3. int? eqValueIndex,
})

Returns true if the values of the sub-lists of list are all equals to eqValue.

  • With eqValueIndex: compares only the value at index eqValueIndex of each sub-list. eqValue defaults to the value at eqValueIndex of the first sub-list.
  • Without eqValueIndex: compares every value of every sub-list (empty sub-lists have no values to compare). eqValue defaults to the first value of the first non-empty sub-list. If all the sub-lists are empty, returns false.

Implementation

bool isListOfListValuesAllEquals(
  Iterable<List>? list, {
  Object? eqValue,
  int? eqValueIndex,
}) {
  if (list == null) return false;

  if (list.isEmpty) return false;

  if (eqValueIndex != null) {
    eqValue ??= list.first[eqValueIndex];
    return listMatchesAll(list, (v) => v[eqValueIndex] == eqValue);
  }

  var hasValues = false;
  for (var l in list) {
    for (var v in l) {
      if (!hasValues) {
        hasValues = true;
        eqValue ??= v;
      }
      if (v != eqValue) return false;
    }
  }

  return hasValues;
}