penultimateOrNull property

E? get penultimateOrNull

Returns the the penultimate (second to last) element in the iterable if it exists, otherwise returns null.

Example:

[1, 2, 3, 4, 5, 6].penultimateOrNull; // 5
[].penultimateOrNull; // null

Implementation

E? get penultimateOrNull => length < 2 ? null : elementAtOrNull(length - 2);