Skip to content

Commit 6bbf54f

Browse files
committed
fix(signal): Add missing getters to Signal of list type.
1 parent 7ee4754 commit 6bbf54f

1 file changed

Lines changed: 26 additions & 21 deletions

File tree

packages/reactter/lib/src/signals/signal_list.dart

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ extension SignalListExt<E> on Signal<List<E>> {
1717
void operator []=(int index, E valueToSet) =>
1818
update((_) => value[index] = valueToSet);
1919

20+
/// Returns the first element.
21+
///
22+
/// Throws a [StateError] if `this` is empty.
23+
/// Otherwise returns the first element in the iteration order,
24+
/// equivalent to `this.elementAt(0)`.
25+
E get first => value.first;
26+
2027
/// The first element of the list.
2128
///
2229
/// The list must be non-empty when accessing its first element.
@@ -35,6 +42,16 @@ extension SignalListExt<E> on Signal<List<E>> {
3542
/// ```
3643
set first(E valueToSet) => update((_) => value.first = valueToSet);
3744

45+
/// Returns the last element.
46+
///
47+
/// Throws a [StateError] if `this` is empty.
48+
/// Otherwise may iterate through the elements and returns the last one
49+
/// seen.
50+
/// Some iterables may have more efficient ways to find the last element
51+
/// (for example a list can directly access the last element,
52+
/// without iterating through the previous ones).
53+
E get last => value.last;
54+
3855
/// The last element of the list.
3956
///
4057
/// The list must be non-empty when accessing its last element.
@@ -53,6 +70,15 @@ extension SignalListExt<E> on Signal<List<E>> {
5370
/// ```
5471
set last(E valueToSet) => update((_) => value.last = valueToSet);
5572

73+
/// The number of objects in this list.
74+
///
75+
/// The valid indices for a list are `0` through `length - 1`.
76+
/// ```dart
77+
/// final numbers = <int>[1, 2, 3];
78+
/// print(numbers.length); // 3
79+
/// ```
80+
int get length => value.length;
81+
5682
/// Setting the `length` changes the number of elements in the list.
5783
///
5884
/// The list must be growable.
@@ -399,27 +425,6 @@ extension SignalListNullExt<E> on Signal<List<E>?> {
399425
update((_) => value?[index] = valueToSet);
400426
}
401427

402-
/// The first element of the list.
403-
///
404-
/// The list must be non-empty when accessing its first element.
405-
///
406-
/// The first element of a list can be modified, unlike an [Iterable].
407-
/// A `list.first` is equivalent to `list[0]`,
408-
/// both for getting and setting the value.
409-
///
410-
/// ```dart
411-
/// final numbers = <int>[1, 2, 3];
412-
/// print(numbers.first); // 1
413-
/// numbers.first = 10;
414-
/// print(numbers.first); // 10
415-
/// numbers.clear();
416-
/// numbers.first; // Throws.
417-
/// ```
418-
set first(E valueToSet) {
419-
if (value == null) return;
420-
update((_) => value?.first = valueToSet);
421-
}
422-
423428
/// The last element of the list.
424429
///
425430
/// The list must be non-empty when accessing its last element.

0 commit comments

Comments
 (0)