Closed thipokch closed 1 year ago
@thipokch in your DemoBloc, you are using emit.forEach
forEach is a method that returns a Future. You must await it inside of the event handler, so the event handler must also be marked as async.
refer to this https://github.com/felangel/bloc/issues/3623#issuecomment-1311005638
change this:
on<EventB>(
(event, emit) =>
emit.forEach(source.numbers, onData: (data) => StateB()),
transformer: restartable());
to this:
on<EventB>((event, emit) async {
await emit.forEach(source.numbers, onData: (data) => StateB())
},
transformer: restartable());
@jacobtipp Thanks!
@jacobtipp @felangel @thipokch
This does not work
@vasilich6107 can you share a link to a minimal reproduction sample? From the screenshot it looks like you’re bucketing EventA and EventB separately so they won’t interfere with each other. I’m guessing you want to bucket them together instead:
on<DemoEvent>((event, emit) async {
if (event is EventA) {…}
else if (event is EventB) {…}
}, transformer: restartable());
hi @felangel I took the data from initial issue description https://github.com/felangel/bloc/issues/3586#issue-1418033166
hi @felangel I took the data from initial issue description #3586 (comment)
See my updated comment and let me know if that helps.
@felangel this was not my code)
I have my own issues with emit.forEach
and restartable
.
I was crawling through the issues and found this one.
I do not know why author closed it as far as suggested approach does not fix the issue.
I will try to find time and create my own example
Thanks for the context above all!
Just trying to fix an issue myself where I want to stop listening to a stream that I started listening to with emit.ForEach.
I tried restartable but that didn't work. The UI dispatches another event, and I want it to stop subscription there. Is this possible?
Hi @talksik
I've created separate issue with reproduction
https://github.com/felangel/bloc/issues/3888
You can add your own reproduction code to my issue.
@vasilich6107 thank you! will keep an eye out there.
I think my problem may have been that I instantiate multiple instances of the same provider. I need this because I want each child widget in a column of widgets to have their own provider. For some reason though, there is a clash and when one of the items in the list of widgets disposes and I call .close, it still continues to listen to the stream that was passed in. (i.e. emit.forEach doesn't stop listening to stream when the provider is closed.)
Description / Expected Behavior
When a new event with
restartable()
is added to a bloc, it is expected that ongoing asynchronous event will be canceled . However, it appears thatemit.forEach
within the same event is still emitting new states when it should be closed.Steps To Reproduce
1. Create the following bloc.
```dart import 'package:bloc_concurrency/bloc_concurrency.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; class DemoBloc extends Bloc2. Create following the bloc test
```dart import 'dart:async'; import 'package:bloc_test/bloc_test.dart'; import 'package:demo/demo/demo_bloc.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:mocktail/mocktail.dart'; class _MockSource extends Mock implements Source {} void main() { group('SessionListBloc', () { late Source source; late StreamController3. Run tests
``` Expected: [Instance of 'StateA', Instance of 'StateB', Instance of 'StateA'] Actual: [Instance of 'StateA', Instance of 'StateB', Instance of 'StateB'] ```demo.zip
Additional Context
I'm a little unsure how concurrency transforms interact with each other. Here's the potential use case.