Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(bloc)!: ignore emitted states after close is called #4337

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions packages/bloc/lib/src/bloc_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,12 @@ abstract class BlocBase<State>
/// To allow for the possibility of notifying listeners of the initial state,
/// emitting a state which is equal to the initial state is allowed as long
/// as it is the first thing emitted by the instance.
///
/// * Throws a [StateError] if the bloc is closed.
@protected
@visibleForTesting
@override
void emit(State state) {
try {
if (isClosed) {
throw StateError('Cannot emit new states after calling close');
}
if (isClosed) return;
if (state == _state && _emitted) return;
onChange(Change<State>(currentState: this.state, nextState: state));
_state = state;
Expand Down
15 changes: 4 additions & 11 deletions packages/bloc/test/bloc_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1540,18 +1540,11 @@ void main() {
await counterBloc.close();
});

test(
'throws StateError and triggers onError '
'when bloc is closed', () async {
test('ignores emitted states when bloc is closed', () async {
Object? capturedError;
StackTrace? capturedStacktrace;

final states = <int>[];
final expectedStateError = isA<StateError>().having(
(e) => e.message,
'message',
'Cannot emit new states after calling close',
);

final counterBloc = CounterBloc(
onErrorCallback: (error, stackTrace) {
Expand All @@ -1565,11 +1558,11 @@ void main() {
expect(counterBloc.isClosed, isTrue);
expect(counterBloc.state, equals(0));
expect(states, isEmpty);
expect(() => counterBloc.emit(1), throwsA(expectedStateError));
expect(() => counterBloc.emit(1), returnsNormally);
expect(counterBloc.state, equals(0));
expect(states, isEmpty);
expect(capturedError, expectedStateError);
expect(capturedStacktrace, isNotNull);
expect(capturedError, isNull);
expect(capturedStacktrace, isNull);
});
});

Expand Down
34 changes: 10 additions & 24 deletions packages/bloc/test/cubit_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -139,30 +139,16 @@ void main() {
});

group('emit', () {
test('throws StateError if cubit is closed', () {
var didThrow = false;
runZonedGuarded(() {
final cubit = CounterCubit();
expectLater(
cubit.stream,
emitsInOrder(<Matcher>[equals(1), emitsDone]),
);
cubit
..increment()
..close()
..increment();
}, (error, _) {
didThrow = true;
expect(
error,
isA<StateError>().having(
(e) => e.message,
'message',
'Cannot emit new states after calling close',
),
);
});
expect(didThrow, isTrue);
test('ignores emitted states when cubit is closed', () {
final cubit = CounterCubit();
expectLater(
cubit.stream,
emitsInOrder(<Matcher>[equals(1), emitsDone]),
);
cubit
..increment()
..close()
..increment();
});

test('emits states in the correct order', () async {
Expand Down
Loading