Skip to content

Instantly share code, notes, and snippets.

@Kavantix
Created October 29, 2021 09:13
Show Gist options
  • Save Kavantix/db35f50bd65dd762bfb431a9f43edb0f to your computer and use it in GitHub Desktop.
Save Kavantix/db35f50bd65dd762bfb431a9f43edb0f to your computer and use it in GitHub Desktop.
Dart view of a list with a map function
import 'dart:collection';
abstract class MappedListView<T, R> implements List<R> {
factory MappedListView(
List<T> wrapped,
R Function(T element) mapper,
) = _MappedListView<T, R>;
}
class _MappedListView<T, R> with ListMixin<R> implements List<R>, MappedListView<T, R> {
_MappedListView(
this.wrapped,
this.mapper,
);
final List<T> wrapped;
final R Function(T element) mapper;
@override
int get length => wrapped.length;
@override
set length(int value) => wrapped.length = value;
@override
R operator [](int index) {
return mapper(wrapped[index]);
}
@override
void operator []=(int index, R value) {
throw UnsupportedError('Assigning to a MappedListView is not supported');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment