dropRightWhile<T> function

List<T> dropRightWhile<T>(
  1. List<T> list,
  2. bool test(
    1. T element
    )
)

starts removing elements from the ending of list until condition becomes false

var list = <int>[2, 1, 3, 4, 5];
list.dropRightWhile((element) => element >= 3); // list = [2, 1];

Implementation

List<T> dropRightWhile<T>(List<T> list, bool Function(T element) test) {
  var result = <T>[];
  if (list.isNotEmpty) {
    var index = 0;
    while (index < list.length) {
      if (!test(list[index])) {
        break;
      }
      result.add(list[index]);
      index++;
    }
  }
  return result;
}