Comments (5)
Why does that throw StateError
in bloc_base.dart line 97
needed? As for me the isClosed
should be handled as in Bloc
(bloc.dart line 202) and it is not interesting to know either stream closed or no. We basically know that if the bloc is initialized then its stream is active, if bloc is closed (due to widget dispose) then any new events should be just discarded and I don't want to see error.
For example, I have a case where I downlooad the image and get its size. It is a long operation which I can break. Now I get a StateError
because of widget is already disposed but emit
is still trying to send new state. To avoid this I need to add additional check on isClosed
in my code. But actually, I repeat, I am not interesting when and why the bloc was closed. I know exactly that it will be closed on widget dispose.
from bloc.
Hi @ra48ad 👋
Thanks for opening an issue!
Are you able to share a link to a minimal reproduction sample illustrating the problem/inconsistency you're seeing? It'd be much easier to help if you could share a repro, thanks!
from bloc.
Hi @ra48ad 👋 Thanks for opening an issue!
Are you able to share a link to a minimal reproduction sample illustrating the problem/inconsistency you're seeing? It'd be much easier to help if you could share a repro, thanks!
Thank you for your reply!
Sure, here are two comparable samples for a Cubit and a Bloc:
Cubit
class CounterCubit extends Cubit<int> {
CounterCubit() : super(0);
// delay emit to simulate async operation
void increment() => Future.delayed(
Duration(seconds: 3),
() => emit(state + 1),
);
}
void main(List<String> args) {
final cubit = CounterCubit();
cubit.increment();
cubit.close();
// prints 0, exception [StateError] thrown (bloc_base.dart line 97)
print(cubit.state);
}
Bloc
enum CounterEvent { increment }
class CounterBloc extends Bloc<CounterEvent, int> {
CounterBloc() : super(0) {
on<CounterEvent>(_onCounterEvent);
}
Future<void> _onCounterEvent(CounterEvent event, Emitter<int> emit) async {
if (event == CounterEvent.increment) {
// delay emit to simulate async operation
return Future.delayed(
Duration(seconds: 3),
() => emit(state + 1),
);
}
}
}
void main(List<String> args) {
final bloc = CounterBloc();
bloc.add(CounterEvent.increment);
bloc.close();
// prints 0, no exception thrown because of isClosed check (bloc.dart line 202)
print(bloc.state);
}
As you can see, there is a (small) difference in behaviour of both, even though the code is very similar.
In a real world app, it could be sometimes easier to use a Cubit instead of a Bloc, but this behaviour leads to writing extra isClosed
checks in the Cubit to prevent the exception.
What do you think? is there anyway to prevent the exception without adding extra checks in the Cubit?
from bloc.
This behavior was introduced to be consistent with how StreamController
works in Dart. I'm open to adjusting the behavior and welcome all feedback from the community 👍
from bloc.
Thank you for your kind reply! @felangel
I understand this point of view and it makes sense. however, since I started using Cubits instead of Blocs in big parts of my apps, this behaviour have been a pain for me and my team. I am glad you are considering a change.
Would it be ok if I make a Pull Request with the change for you to review it?
My suggestion would be to simply return if the cubit is closed (in BlocBase
), just like Bloc
. What do you think?
from bloc.
Related Issues (20)
- Emit that occurs during init doesn't cause a rebuild HOT 1
- How to rely on data from another Stream? HOT 4
- question: is this considered a anti pattern if a repository depends on another bloc. HOT 2
- docs: RTL in documentation
- feat: allow user to choose which event or state change to be captured in the changeStack or not HOT 2
- feat: Translation of menu and prev, next, note title in the document HOT 1
- question: UI interactivity and separation of concerns HOT 2
- question: Having a bloc inherit from a another bloc HOT 1
- style: Move the "props" getter to the parent (sealed) class HOT 1
- fix: How to listen to multiple streams in a single event? HOT 1
- feat: can we rename copyWith to replaceWith HOT 1
- fix: com.bloc.intellij_generator_plugin.action.GenerateBlocAction retains disposed Project HOT 3
- feat: Enhance BlocBuilder with Dependencies for Selective State Changes HOT 1
- [help] Best Practice for Sharing Events and States Across Multiple Blocs, Bloc to Bloc communications HOT 8
- fix: BlocBuilder doesn't receive state when emitted from async loop HOT 4
- How to inspect bloc in Flutter Inspector? HOT 1
- How to deal with assert(false) in blocTest? HOT 2
- feat: Test support for BlocObservers HOT 10
- build: Update test dependency of bloc_test package from 1.16.6 to the latest version HOT 4
- feat: support testing of `emit.forEach` HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bloc.