Skip to content

Commit

Permalink
refactor: make onError StackTrace non-nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
felangel committed Oct 7, 2020
1 parent 8dbac90 commit 5e96fa5
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 20 deletions.
2 changes: 1 addition & 1 deletion packages/bloc/example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class SimpleBlocObserver extends BlocObserver {
}

@override
void onError(Cubit cubit, Object error, StackTrace? stackTrace) {
void onError(Cubit cubit, Object error, StackTrace stackTrace) {
print('cubit: ${cubit.runtimeType}, error: $error');
super.onError(cubit, error, stackTrace);
}
Expand Down
12 changes: 5 additions & 7 deletions packages/bloc/lib/src/bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ abstract class Bloc<Event, State> extends Cubit<State>
/// The current [BlocObserver].
static BlocObserver observer = BlocObserver();

late final _eventController = StreamController<Event>.broadcast();
final _eventController = StreamController<Event>.broadcast();

StreamSubscription<Transition<Event, State>>? _transitionSubscription;
late StreamSubscription<Transition<Event, State>> _transitionSubscription;

bool _emitted = false;

Expand Down Expand Up @@ -169,14 +169,12 @@ abstract class Bloc<Event, State> extends Cubit<State>
/// Notifies the [Bloc] of an [error] which triggers [onError].
@override
void addError(Object error, [StackTrace? stackTrace]) {
onError(error, stackTrace);
onError(error, stackTrace ?? StackTrace.current);
}

/// Called whenever an [error] is thrown within [mapEventToState].
/// By default all [error]s will be ignored and [Bloc] functionality will be
/// unaffected.
/// The [stackTrace] argument may be `null` if the [state] stream received
/// an error without a [stackTrace].
/// A great spot to handle errors at the individual [Bloc] level.
///
/// **Note: `super.onError` should always be called last.**
Expand All @@ -197,7 +195,7 @@ abstract class Bloc<Event, State> extends Cubit<State>
@protected
@mustCallSuper
@override
void onError(Object error, StackTrace? stackTrace) {
void onError(Object error, StackTrace stackTrace) {
super.onError(error, stackTrace);
}

Expand All @@ -211,7 +209,7 @@ abstract class Bloc<Event, State> extends Cubit<State>
@mustCallSuper
Future<void> close() async {
await _eventController.close();
await _transitionSubscription?.cancel();
await _transitionSubscription.cancel();
return super.close();
}

Expand Down
2 changes: 1 addition & 1 deletion packages/bloc/lib/src/bloc_observer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ class BlocObserver {
/// an error without a [stackTrace].
@protected
@mustCallSuper
void onError(Cubit cubit, Object error, StackTrace? stackTrace) {}
void onError(Cubit cubit, Object error, StackTrace stackTrace) {}
}
6 changes: 2 additions & 4 deletions packages/bloc/lib/src/cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ abstract class Cubit<State> extends Stream<State> {

/// Notifies the [Cubit] of an [error] which triggers [onError].
void addError(Object error, [StackTrace? stackTrace]) {
onError(error, stackTrace);
onError(error, stackTrace ?? StackTrace.current);
}

/// Called whenever a [change] occurs with the given [change].
Expand Down Expand Up @@ -117,8 +117,6 @@ abstract class Cubit<State> extends Stream<State> {
/// Called whenever an [error] occurs within a [Cubit].
/// By default all [error]s will be ignored and [Cubit] functionality will be
/// unaffected.
/// The [stackTrace] argument may be `null` if the [state] stream received
/// an error without a [stackTrace].
/// A great spot to handle errors at the individual [Cubit] level.
///
/// **Note: `super.onError` should always be called last.**
Expand All @@ -133,7 +131,7 @@ abstract class Cubit<State> extends Stream<State> {
/// ```
@protected
@mustCallSuper
void onError(Object error, StackTrace? stackTrace) {
void onError(Object error, StackTrace stackTrace) {
// ignore: invalid_use_of_protected_member
_observer.onError(this, error, stackTrace);
assert(() {
Expand Down
6 changes: 3 additions & 3 deletions packages/bloc/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: bloc
description: A predictable state management library that helps implement the BLoC (Business Logic Component) design pattern.
version: 6.1.0-nullsafety
version: 7.0.0-nullsafety
repository: https://github.com/felangel/bloc/tree/master/packages/bloc
issue_tracker: https://github.com/felangel/bloc/issues
homepage: https://github.com/felangel/bloc
Expand All @@ -10,16 +10,16 @@ documentation: https://bloclibrary.dev
publish_to: none

environment:
sdk: ">=2.10.0-99.0.dev <2.10.0"
sdk: ">=2.10.0-99.0.dev <2.11.0"

dependencies:
meta: ^1.3.0-nullsafety.2

dev_dependencies:
coverage: ^0.14.1
pedantic: ^1.9.0
rxdart:
git:
url: https://github.com/ReactiveX/rxdart
ref: nnbd
test_coverage: ^0.4.1
test: ^1.16.0-nullsafety.2
2 changes: 1 addition & 1 deletion packages/bloc/test/blocs/counter/counter_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class CounterBloc extends Bloc<CounterEvent, int> {
}

@override
void onError(Object error, StackTrace? stackTrace) {
void onError(Object error, StackTrace stackTrace) {
onErrorCallback?.call(error, stackTrace);
super.onError(error, stackTrace);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/bloc/test/blocs/counter/on_error_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class OnErrorBloc extends Bloc<CounterEvent, int> {
final Error error;

@override
void onError(Object error, StackTrace? stackTrace) {
void onError(Object error, StackTrace stackTrace) {
onErrorCallback(error, stackTrace);
super.onError(error, stackTrace);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/bloc/test/blocs/counter/on_exception_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class OnExceptionBloc extends Bloc<CounterEvent, int> {
final Exception exception;

@override
void onError(Object error, StackTrace? stackTrace) {
void onError(Object error, StackTrace stackTrace) {
onErrorCallback(error, stackTrace);
super.onError(error, stackTrace);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class OnTransitionErrorBloc extends Bloc<CounterEvent, int> {
final Error error;

@override
void onError(Object error, StackTrace? stackTrace) {
void onError(Object error, StackTrace stackTrace) {
onErrorCallback(error, stackTrace);
super.onError(error, stackTrace);
}
Expand Down
13 changes: 13 additions & 0 deletions packages/bloc/test/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'bloc_observer_test.dart' as bloc_observer_test;
import 'bloc_test.dart' as bloc_test;
import 'change_test.dart' as change_test;
import 'cubit_test.dart' as cubit_test;
import 'transition_test.dart' as transition_test;

void main() {
bloc_test.main();
bloc_observer_test.main();
change_test.main();
transition_test.main();
cubit_test.main();
}

0 comments on commit 5e96fa5

Please sign in to comment.