rx_notifier 0.0.4 copy "rx_notifier: ^0.0.4" to clipboard
rx_notifier: ^0.0.4 copied to clipboard

outdated

Extension to ValueNotifier by transparently applying functional reactive programming (TFRP)

rx_notifier #

The ValueNotifier is a simple, native form of Flutter reactivity. This extension aims to transparently apply functional reactive programming (TFRP).

Install #

Add in pubspec.yaml:

dependencies:
  rx_notifier: <version>

Understanding Extension. #

This extension adds a class RxNotifier and a converter ValueNotifier -> RxNotifier so that it can be observed transparently by the function rxObserver() and Widget RxBuilder.

The RxNotifier is directly an extension of ValueListenable then any object that implements it can be converted to RxNotifier

The only difference from RxNofifier to ValueNotifier is the automatic signature function in Observers rxObserver() e RxBuilder, very similar to MobX reactions.

Using #

To start, instantiate an RxNofifier.

final counter = RxNotifier<int>(0);

or convert a ValueNotifier already existing using the .rx() method:


final counter = myValueNotifierCounter.rx();

IMPORTANT: The rx() method has been added to ValueNotifier using Extension Methods.

We assign a new value:


final counter = RxNotifier<int>(0);

increment(){
    counter.value++;
}

And we hear the changes using rxObserver:


RxDisposer disposer = rxObserver((){
    print(counter.value);
});


disposer();

Or we listen in the Widget tree using RxBuilder:


Widget builder(BuildContext context){
    return RxBuilder(
        builder: (_) => Text('${counter.value}'),
    );
}

This is the transparent use of individual reactivity, but we can also combine RxNotifier Objects producing new value. This technique is called Computed

Computed: Combining reactive values #

To combine two or more RxNotifier Objects we need to use a getter returning a new combined value:


final num1 = RxNotifier<int>(1);
final num2 = RxNotifier<int>(2);

String get result => 'num1: ${num1.value} + num2: ${num2.value} = ${num1.value + num2.value}';

...

rxObserver((){
    print(result); // print´s "num1: 1 + num2: 2 = 3
});


IMPORTANT: It is really necessary that computed are Getters and not assignments. The reaction will happen when any of the RxNotifier changes the value.

Using Getters #

We can also use getters in reactive values making, let's repeat the example above:


final _num1 = RxNotifier<int>(1);
int get num1 => _num1.value;

final _num2 = RxNotifier<int>(2);
int get num2 => _num2.value;

String get result => 'num1: $num1 + num2: $num2 = ${num1 + num2}';

...

rxObserver((){
    print(result); // print´s "num1: 1 + num2: 2 = 3
});


Filters #

Both rxObserver and RxBuilder have a property filter filter which is a function that returns a bool. Use this to define when (or not) to reflect changes:


Widget builder(BuildContext context){
    return RxBuilder(
        filter: () => counter.value < 10,
        builder: (_) => Text('${counter.value}'),
    );
}

Empower more the ValueNotifier #

The functional_listener add map, where, listen, debounce, combineLatest and several other functions for ValueNotifier. Thank you very much Thomas Burkhart for that!!!

Features and bugs #

Please send feature requests and bugs at the issue tracker.

This README was created based on templates made available by Stagehand under a BSD-style license.

82
likes
0
pub points
87%
popularity

Publisher

verified publisherflutterando.com.br

Extension to ValueNotifier by transparently applying functional reactive programming (TFRP)

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

flutter

More

Packages that depend on rx_notifier