forEach<R> method

void forEach<R>(
  1. T function(
    1. R val
    )
)

Iterates over each element in the array and applies the provided function. The function takes an element of type R as input and returns a value of type T. function A function that takes an element of type R and returns a value of type T. Example:

ModelLessArray<int> array = ModelLessArray<int>();
array.set(1);
array.set(2);
array.set(3);
array.forEach<int>((val) {
  print(val); // Outputs: 1, 2, 3
  return val * 2; // Example of returning a value, though it's not used here
});

Implementation

void forEach<R>(T Function(R val) function) {
  for (int i = 0; i < length; i++) {
    function(get<R>(i));
  }
}