rxdart_ext

Author: Petrus Nguyễn Thái Học

codecov Dart CI Pub Version (including pre-releases) Hits GitHub Style

Some extension methods and classes built on top of RxDart - RxDart extension.

RxDart compatibility

rxdartrxdart_ext
0.26.0<=0.0.1
>=0.27.0 <=0.27.1>=0.1.0 <=0.1.1
>=0.27.2>=0.1.2

API

1. Single

A Stream which emits single event, either data or error, and then close with a done-event.

Success case: ------------(*)|------
                         data done

Failure case: ------------(x)|------
                        error done

NOTE: Single extends Stream, so all operators and transformers for Stream are available for Single as well.

Single is suitable for one-shot operations (likes Future but lazy - executes when listening), eg. making API request, reading local storage, ...

import 'package:http/http.dart' as http;

Single<User> fetchUser(String id) {
  return Single.fromCallable(() => http.get(Uri.parse('$baseUrl/users/$id')))
      .flatMapSingle((res) => res.statusCode == HttpStatus.ok
          ? Single.value(res.body)
          : Single.error(Exception('Cannot fetch user with id=$id')))
      .map((body) => User.fromJson(jsonEncode(body)));
}

2. Operators for Stream

3. StateStream

A Stream that provides synchronous access to the last emitted item, and two consecutive values are not equal. The equality between previous data event and current data event is determined by StateStream.equals. This Stream always has no error.

Example

Useful for Flutter BLoC pattern - StreamBuilder, expose broadcast state stream to UI, can synchronous access to the last emitted item, and distinct until changed

  • x Distinct: distinct until changed.
  • x Value: can synchronous access to the last emitted item.
  • x NotReplay: not replay the latest value.
  • x Connectable: broadcast stream - can be listened to multiple time.
                                Stream (dart:core)
                                   ^
                                   |
                                   |
            |--------------------------------------------|
            |                                            |
            |                                            |
        ValueStream (rxdart)                             |
            ^                                            |
            |                                            |
            |                                            |
    NotReplayValueStream (rxdart_ext)                    |
            ^                                    ConnectableStream (rxdart)
            |                                            ^
            |                                            |
       StateStream (rxdart_ext)                          |
            ^                                            |
            |                                            |
            |------------                     -----------|
                        |                     |
                        |                     |
                     StateConnectableStream (rxdart_ext)
class UiState { ... }

final Stream<UiState> state$ = ...;

final StateConnectableStream<UiState> state$ = state$.publishState(UiState.initial());
final connection = state$.connect();

StreamBuilder<UiState>(
  initialData: state$.value,
  stream: state$,
  builder: (context, snapshot) {
    final UiState state = snapshot.requireData;
    
    return ...;
  },
);

4. NotReplayValueStream

A Stream that provides synchronous access to the last emitted item, but not replay the latest value.

License

MIT License

Copyright (c) 2020-2021 Petrus Nguyễn Thái Học

Tim Cook dancing to the sound of a permissive license.

Libraries

not_replay_value_stream
operators
rxdart_ext
Some extensions built on top of RxDart. [...]
single
state_stream
utils