derive<Derived> method
Creates a derived subject that projects a subset of this subject's value.
The derived subject updates whenever this subject updates, applying
deriveState to produce its value. The derived subject cannot have
values added directly.
final nameSubject = userSubject.derive((user) => user.name);
Implementation
CurrentValueSubject<Derived> derive<Derived>(
Derived Function(T state) deriveState,
) {
final derivedSubject = CurrentValueSubject<Derived>._(
initialValue: _value == null ? null : deriveState(_value as T),
options: const SubjectOptions(isDerived: true),
);
final id = listen(
(newValue) => derivedSubject._internalAdd(deriveState(newValue)),
);
derivedSubject.onCancel = () => cancel(id);
return derivedSubject;
}