chainIf method

T? chainIf(
  1. bool shouldChain
)

Allows conditional method chaining based on a condition.

Returns this object if shouldChain is true, null otherwise.

This is intended to be used with the conditional-member-access operator (?.).

Example:

var items = Set<int>.of([1, 2, 3]..chainIf(shouldShuffle)?.shuffle());

Implementation

// ignore: avoid_positional_boolean_parameters, https://github.com/dart-lang/linter/issues/1638
T? chainIf(bool shouldChain) => shouldChain ? this : null;