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

feat: method for safe emission of new states #3403

Closed
tomassasovsky opened this issue Jun 20, 2022 · 4 comments
Closed

feat: method for safe emission of new states #3403

tomassasovsky opened this issue Jun 20, 2022 · 4 comments
Labels
discussion Open discussion for a specific topic duplicate This issue or pull request already exists

Comments

@tomassasovsky
Copy link
Contributor

Description
In personal and work projects, we use a lot a method that we basically copy-paste for every Bloc/Cubit. This is it:

  void safeEmit(State state) {
    if (isClosed) return;
    emit(state);
  }

This avoids new states to be emitted after closing the Bloc, so the Bad state error doesn't get thrown.

Desired Solution
Ideally, add the safeEmit function to the BlocBase class.

@AlexanderFarkas
Copy link

It's complicated for BLoC since emit is actually Emitter callable class.

You could just use extensions methods:

extension EmitterX<T> on Emitter<T> {
  void safe(State state) {
    if (isClosed) return;
    emit(state);
  }
}

Usage:

on<Event>((event, emit) => emit.safe(MyState());

For cubit:

extension CubitX<T> on Cubit<T> {
  void safeEmit(State state) {
    if (isClosed) return;
    emit(state);
  }
}

Usage:

safeEmit(MyState());

For Cubit you also could use mixins.

@tenhobi
Copy link
Collaborator

tenhobi commented Mar 11, 2023

This is indeed a common case people talk about often. Similarly, in Flutter, you can see navigator.pop() and navigator.canPop() or BuildContext and its context.mounted property.

It is hard to say whether some way of safeEmit or integrating it into the emit method is conceptually the right way we should do it.

While it makes sense, it can hide potential errors, like making navigator.safePop() etc. The issue is that especially after an async task, you cannot be sure about the state of bloc's stream. (similarly, as you cannot be sure whether a widget is mounted or not)

We could suppress it directly in the emit method by checking each time, but that hides all the issues. And I do not think it is the best thing to do as part of this main library. You can always override it by using extensions as stated above in your project.

@tenhobi tenhobi added the discussion Open discussion for a specific topic label Mar 11, 2023
@nogicoder
Copy link

It's complicated for BLoC since emit is actually Emitter callable class.

You could just use extensions methods:

extension EmitterX<T> on Emitter<T> {
  void safe(State state) {
    if (isClosed) return;
    emit(state);
  }
}

Usage:

on<Event>((event, emit) => emit.safe(MyState());

The isClosed property is from Bloc, not from Emitter

@felangel
Copy link
Owner

Closing this since it's a duplicate of #4165

@felangel felangel added the duplicate This issue or pull request already exists label Jul 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion Open discussion for a specific topic duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

5 participants