Created
December 15, 2021 12:21
-
-
Save xsahil03x/18d53454b2c996a8a23b5d40a4d36c34 to your computer and use it in GitHub Desktop.
SelectValueNotifier
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class _SelectValueNotifier<R, T> extends ValueNotifier<R> { | |
_SelectValueNotifier(this.source, this.selector) | |
: super(selector(source.value)) { | |
source.addListener(_sourceListener); | |
} | |
final ValueNotifier<T> source; | |
final R Function(T value) selector; | |
void _sourceListener() => value = selector(source.value); | |
@override | |
void dispose() { | |
source.removeListener(_sourceListener); | |
super.dispose(); | |
} | |
} | |
extension SelectValueNotifier<T> on ValueNotifier<T> { | |
ValueNotifier<R> select<R>(R Function(T value) selector) => | |
_SelectValueNotifier<R, T>(this, selector); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
OR