Skip to content

Commit 840845d

Browse files
authored
Merge pull request #1 from Flutter-Dart-Open-Source/nnbd
nnbd
2 parents c51c5b9 + b521026 commit 840845d

File tree

8 files changed

+98
-66
lines changed

8 files changed

+98
-66
lines changed

.github/workflows/flutter.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ jobs:
1414
- uses: actions/checkout@v2
1515

1616
- uses: subosito/flutter-action@v1.4.0
17+
with:
18+
channel: 'dev'
1719

1820
- name: Doctor
1921
run: flutter doctor

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.1.0-nullsafety.0 - Jan 15, 2021
2+
3+
- Migrate this package to null safety.
4+
- Sdk constraints: `>=2.12.0-0 <3.0.0` based on beta release guidelines.
5+
16
## 0.0.2 - Jan 15, 2021
27

38
- Internal refactor

lib/src/common.dart

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ Stream<R> toStreamWithTransform<T extends Listenable, R>(
88
T listenable,
99
R Function(T) transform,
1010
) {
11-
StreamController<R> controller;
12-
VoidCallback listener;
11+
final controller = StreamController<R>();
12+
VoidCallback? listener;
1313

14-
final onListenOrOnResume = () {
14+
controller.onListen = () {
1515
assert(listener == null);
1616
try {
1717
listenable
@@ -21,26 +21,13 @@ Stream<R> toStreamWithTransform<T extends Listenable, R>(
2121
}
2222
};
2323

24-
final createOnPauseOrOnCancel = ([bool closeOnError = false]) {
25-
return () {
26-
assert(listener != null);
27-
try {
28-
listenable.removeListener(listener);
29-
listener = null;
30-
} catch (_ /*Ignore*/) {
31-
if (identical(closeOnError, true)) {
32-
controller.close();
33-
}
34-
}
35-
};
24+
controller.onCancel = () {
25+
assert(listener != null);
26+
try {
27+
listenable.removeListener(listener!);
28+
listener = null;
29+
} catch (_ /*Ignore*/) {}
3630
};
3731

38-
controller = StreamController<R>(
39-
onListen: onListenOrOnResume,
40-
onPause: createOnPauseOrOnCancel(true),
41-
onResume: onListenOrOnResume,
42-
onCancel: createOnPauseOrOnCancel(),
43-
);
44-
4532
return controller.stream;
4633
}

lib/src/streamx.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ extension StartWithExtension<T> on Stream<T> {
77
/// Prepends a value to the source [Stream].
88
Stream<T> startWith(T Function() seededProvider) {
99
final controller = StreamController<T>(sync: true);
10-
StreamSubscription<T> subscription;
10+
late StreamSubscription<T> subscription;
1111

1212
controller.onListen = () {
1313
controller.add(seededProvider());
Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import 'dart:async' show Stream, StreamSubscription;
22

33
import 'package:flutter/foundation.dart' show ValueListenable;
4-
import 'package:rxdart/rxdart.dart' show ErrorAndStackTrace, ValueStream;
4+
import 'package:rxdart/rxdart.dart' show ValueStream, ValueWrapper;
55

66
import 'common.dart';
77
import 'streamx.dart';
@@ -22,40 +22,25 @@ extension ValueListenableToValueStream<T> on ValueListenable<T> {
2222
///
2323
/// If [replayValue] is true, the returned [ValueStream] will replay latest value when listening to it.
2424
/// Otherwise, it does not.
25-
ValueStream<T> toValueStream({bool replayValue = false}) =>
25+
ValueListenableStream<T> toValueStream({bool replayValue = false}) =>
2626
ValueListenableStream<T>(this, replayValue);
2727
}
2828

2929
/// A Single-Subscription Stream will emits data when [ValueListenable.value] changed.
3030
class ValueListenableStream<T> extends Stream<T> implements ValueStream<T> {
3131
final ValueListenable<T> _valueListenable;
3232
final bool _replayValue;
33-
Stream<T> _stream;
33+
Stream<T>? _stream;
3434

3535
/// Construct a [ValueListenableStream] from [ValueListenable].
3636
ValueListenableStream(this._valueListenable, this._replayValue);
3737

38-
@override
39-
bool get isBroadcast => false;
40-
41-
@override
42-
ErrorAndStackTrace get errorAndStackTrace => null;
43-
44-
@override
45-
bool get hasError => false;
46-
47-
@override
48-
bool get hasValue => true;
49-
50-
@override
51-
T get value => _valueListenable.value;
52-
5338
@override
5439
StreamSubscription<T> listen(
55-
void Function(T) onData, {
56-
Function onError,
57-
void Function() onDone,
58-
bool cancelOnError,
40+
void Function(T)? onData, {
41+
Function? onError,
42+
void Function()? onDone,
43+
bool? cancelOnError,
5944
}) {
6045
if (_replayValue) {
6146
_stream ??=
@@ -64,11 +49,21 @@ class ValueListenableStream<T> extends Stream<T> implements ValueStream<T> {
6449
_stream ??= _valueListenable.toStream();
6550
}
6651

67-
return _stream.listen(
52+
return _stream!.listen(
6853
onData,
6954
onError: onError,
7055
onDone: onDone,
7156
cancelOnError: cancelOnError,
7257
);
7358
}
59+
60+
@override
61+
bool get isBroadcast => false;
62+
63+
@override
64+
Never get errorAndStackTrace =>
65+
throw StateError('This Stream always has no error!');
66+
67+
@override
68+
ValueWrapper<T> get valueWrapper => ValueWrapper(_valueListenable.value);
7469
}

pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ packages:
9494
name: rxdart
9595
url: "https://pub.dartlang.org"
9696
source: hosted
97-
version: "0.25.0"
97+
version: "0.26.0-nullsafety.1"
9898
sky_engine:
9999
dependency: transitive
100100
description: flutter

pubspec.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
name: listenable_stream
22
description: Convert Listenable (eg. ChangeNotifier), ValueListenable(eg. ValueNotifier) to Stream / ValueStream.
3-
version: 0.0.2
3+
version: 0.1.0-nullsafety.0
44
author: Petrus Nguyễn Thái Học <hoc081098@gmail.com>
55
homepage: https://github.com/Flutter-Dart-Open-Source/listenable_stream.git
66
repository: https://github.com/Flutter-Dart-Open-Source/listenable_stream.git
77
issue_tracker: https://github.com/Flutter-Dart-Open-Source/listenable_stream/issues
88

99
environment:
10-
sdk: ">=2.7.0 <3.0.0"
10+
sdk: ">=2.12.0-0 <3.0.0"
1111
flutter: ">=1.17.0"
1212

1313
dependencies:
14-
rxdart: ^0.25.0
14+
rxdart: ^0.26.0-nullsafety.1
1515
flutter:
1616
sdk: flutter
1717

0 commit comments

Comments
 (0)