Skip to content

Commit 319dc68

Browse files
authored
2.1.0 (#3)
* 3.0.0 * update * update * update * update * update * update ci * update ci * rm assert not null
1 parent c5dcce2 commit 319dc68

File tree

12 files changed

+166
-147
lines changed

12 files changed

+166
-147
lines changed

.github/workflows/flutter-test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515

1616
- uses: subosito/[email protected]
1717
with:
18-
channel: 'beta'
18+
channel: 'stable'
1919

2020
- name: Doctor
2121
run: flutter doctor

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 2.1.0 - May 10, 2021
2+
3+
- Update `rxdart` to `0.27.0`.
4+
- `RxStreamBuilder` now accepts a `ValueStream`.
5+
16
## 2.0.0 - Mar 3, 2021
27

38
- Stable release for null safety.

example/lib/counter_bloc.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class CounterBloc extends DisposeCallbackBaseBloc {
2222
final incrementController = StreamController<void>();
2323

2424
final state = incrementController.stream
25-
.scan<int>((acc, _, __) => acc! + 1, 0)
25+
.scan<int>((acc, _, __) => acc + 1, 0)
2626
.publishValueDistinct(0);
2727
final connection = state.connect();
2828

example/pubspec.lock

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@ packages:
4949
name: cupertino_icons
5050
url: "https://pub.dartlang.org"
5151
source: hosted
52-
version: "1.0.2"
52+
version: "1.0.3"
5353
distinct_value_connectable_stream:
5454
dependency: "direct main"
5555
description:
5656
name: distinct_value_connectable_stream
5757
url: "https://pub.dartlang.org"
5858
source: hosted
59-
version: "1.2.0-nullsafety.5"
59+
version: "1.3.0"
6060
fake_async:
6161
dependency: transitive
6262
description:
@@ -75,7 +75,7 @@ packages:
7575
path: ".."
7676
relative: true
7777
source: path
78-
version: "2.0.0"
78+
version: "2.1.0"
7979
flutter_provider:
8080
dependency: transitive
8181
description:
@@ -115,21 +115,21 @@ packages:
115115
name: pedantic
116116
url: "https://pub.dartlang.org"
117117
source: hosted
118-
version: "1.10.0"
118+
version: "1.11.0"
119119
rxdart:
120120
dependency: transitive
121121
description:
122122
name: rxdart
123123
url: "https://pub.dartlang.org"
124124
source: hosted
125-
version: "0.26.0"
125+
version: "0.27.0"
126126
rxdart_ext:
127127
dependency: "direct main"
128128
description:
129129
name: rxdart_ext
130130
url: "https://pub.dartlang.org"
131131
source: hosted
132-
version: "0.0.1"
132+
version: "0.1.0"
133133
sky_engine:
134134
dependency: transitive
135135
description: flutter
@@ -141,7 +141,7 @@ packages:
141141
name: source_span
142142
url: "https://pub.dartlang.org"
143143
source: hosted
144-
version: "1.8.0"
144+
version: "1.8.1"
145145
stack_trace:
146146
dependency: transitive
147147
description:
@@ -192,4 +192,4 @@ packages:
192192
source: hosted
193193
version: "2.1.0"
194194
sdks:
195-
dart: ">=2.12.0-0.0 <3.0.0"
195+
dart: ">=2.12.0 <3.0.0"

example/pubspec.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ environment:
77
sdk: ">=2.12.0-0 <3.0.0"
88

99
dependencies:
10-
distinct_value_connectable_stream: ^1.2.0-nullsafety.5
11-
rxdart_ext: ^0.0.1
10+
distinct_value_connectable_stream: ^1.3.0
11+
rxdart_ext: ^0.1.0
1212
flutter_bloc_pattern:
1313
flutter:
1414
sdk: flutter
15-
cupertino_icons: ^1.0.2
15+
cupertino_icons: ^1.0.3
1616

1717
dev_dependencies:
18-
pedantic: ^1.10.0
18+
pedantic: ^1.11.0
1919
flutter_test:
2020
sdk: flutter
2121

lib/src/base.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ class DisposeCallbackBaseBloc implements BaseBloc {
1010
final void Function() _dispose;
1111

1212
/// Create a [DisposeCallbackBaseBloc] by a dispose callback.
13-
// ignore: unnecessary_null_comparison
14-
DisposeCallbackBaseBloc(this._dispose) : assert(_dispose != null);
13+
DisposeCallbackBaseBloc(this._dispose);
1514

1615
@override
1716
void dispose() => _dispose();

lib/src/provider.dart

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import 'package:flutter_provider/flutter_provider.dart';
44
import 'base.dart';
55
import 'error.dart';
66

7-
// ignore_for_file: unnecessary_null_comparison
8-
97
/// Provides [BaseBloc] to all descendants of this Widget. This should
108
/// generally be a root widget in your App
119
class BlocProvider<T extends BaseBloc> extends StatelessWidget {
@@ -116,8 +114,7 @@ class BlocProviders extends Providers {
116114
Key? key,
117115
required List<BlocProvider> blocProviders,
118116
required Widget child,
119-
}) : assert(blocProviders != null && blocProviders.isNotEmpty),
120-
assert(child != null),
117+
}) : assert(blocProviders.isNotEmpty),
121118
super(
122119
key: key,
123120
providers:

lib/src/rx_stream_builder.dart

Lines changed: 61 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@ import 'package:rxdart/rxdart.dart';
55

66
import 'error.dart';
77

8-
// ignore_for_file: unnecessary_null_comparison
9-
108
/// Signature for strategies that build widgets based on asynchronous interaction.
11-
typedef RxWidgetBuilder<T> = Widget Function(BuildContext context, T? data);
9+
typedef RxWidgetBuilder<T> = Widget Function(BuildContext context, T data);
1210

1311
/// Rx stream builder that will pre-populate the streams initial data if the
1412
/// given stream is an stream that holds the streams current value such
1513
/// as a [ValueStream] or a [ReplayStream]
16-
class RxStreamBuilder<T> extends StreamBuilder<T> {
14+
class RxStreamBuilder<T> extends StatefulWidget {
15+
final RxWidgetBuilder<T> _builder;
16+
final ValueStream<T> _stream;
17+
1718
/// Creates a new [RxStreamBuilder] that builds itself based on the latest
1819
/// snapshot of interaction with the specified [stream] and whose build
1920
/// strategy is given by [builder].
@@ -25,45 +26,71 @@ class RxStreamBuilder<T> extends StreamBuilder<T> {
2526
/// effects as it may be called multiple times.
2627
RxStreamBuilder({
2728
Key? key,
29+
required ValueStream<T> stream,
2830
required RxWidgetBuilder<T> builder,
29-
required Stream<T> stream,
30-
T? initialData,
31-
}) : assert(builder != null),
32-
assert(stream != null),
33-
super(
34-
key: key,
35-
initialData: getInitialData(initialData, stream),
36-
builder: _createStreamBuilder<T>(builder),
37-
stream: stream,
38-
);
31+
}) : _builder = builder,
32+
_stream = stream;
33+
34+
@override
35+
_RxStreamBuilderState<T> createState() => _RxStreamBuilderState();
3936

4037
/// Get latest value from stream or return `null`.
4138
@visibleForTesting
42-
static T? getInitialData<T>(T? initialData, Stream<T> stream) {
43-
if (initialData != null) {
44-
return initialData;
39+
static T getInitialData<T>(ValueStream<T> stream) {
40+
if (stream.hasValue) {
41+
return stream.value;
4542
}
43+
throw ArgumentError.value(stream, 'stream', 'does not have value');
44+
}
45+
}
4646

47-
if (stream is ValueStream<T> && stream.hasValue) {
48-
return stream.requireValue;
49-
}
47+
class _RxStreamBuilderState<T> extends State<RxStreamBuilder<T>> {
48+
late T value;
49+
StreamSubscription<T>? subscription;
50+
51+
@override
52+
void initState() {
53+
super.initState();
54+
subscribe();
55+
}
5056

51-
if (stream is ReplayStream<T>) {
52-
final values = stream.values;
53-
if (values.isNotEmpty) {
54-
return values.last;
55-
}
57+
@override
58+
void didUpdateWidget(covariant RxStreamBuilder<T> oldWidget) {
59+
super.didUpdateWidget(oldWidget);
60+
if (oldWidget._stream != widget._stream) {
61+
unsubscribe();
62+
subscribe();
5663
}
64+
}
65+
66+
@override
67+
void dispose() {
68+
unsubscribe();
69+
super.dispose();
70+
}
5771

58-
return null;
72+
@override
73+
Widget build(BuildContext context) => widget._builder(context, value);
74+
75+
void subscribe() {
76+
value = RxStreamBuilder.getInitialData(widget._stream);
77+
78+
subscription = widget._stream.listen(
79+
(v) => setState(() => value = v),
80+
onError: (Object e, StackTrace s) {
81+
FlutterError.reportError(
82+
FlutterErrorDetails(
83+
exception: UnhandledStreamError(e),
84+
stack: s,
85+
library: 'flutter_bloc_pattern',
86+
),
87+
);
88+
},
89+
);
5990
}
6091

61-
static AsyncWidgetBuilder<T> _createStreamBuilder<T>(
62-
RxWidgetBuilder<T> builder) =>
63-
(context, snapshot) {
64-
if (snapshot.hasError) {
65-
throw UnhandledStreamError(snapshot.error!);
66-
}
67-
return builder(context, snapshot.data);
68-
};
92+
void unsubscribe() {
93+
subscription?.cancel();
94+
subscription = null;
95+
}
6996
}

0 commit comments

Comments
 (0)