Bloc: BlocListener emits multiple same states causing navigated screen to build multiple times .

Created on 23 Sep 2020  路  4Comments  路  Source: felangel/bloc

Describe the bug
I have a screen that calls a view event on a blog from my bloc which updates a field value if firebase and then emits a viewsuccess state .I then use the bloclistener to list for the success state for which it should navigate to blog's detailed page and then passing the id of the blogdetailed through a contractor to to call a new bloc of which emits a get blog using the id passed but when I am moving back it seem to have built multiple version of the detailed screen about 3 times . and upon Debug it seem to be coming from the listener which emits the view success state multiple times .
Question
why is this happening ?
Is it because if didn't scope the two different instances well ,if so how would I scope it cos I have used every means I could find online.

All Codes

event handling viewEvent
if (event is ViewPost) { try { await _blogRepo.getAndUpdateViews(id: event.postId); yield ViewSuccess(); } catch (e) { yield ViewFailed(err: e); } }

code that update the firebase
Future<int> getAndUpdateViews({@required String id}) async { await _collection.document(id).updateData({VIEWS: FieldValue.increment(1)}); return 0;

BlocListener and BlocBuilder
Screenshot 2020-09-23 at 10 02 00 AM

Screenshot 2020-09-23 at 10 02 29 AM

methods to handle navigation
void nextScreen(index, id) { var info = {'index': index, 'blogPost': id}; detailedBlog = info; }

void goToDetailed(context) { Navigator.of(context).pushNamed('/blogDetailed', arguments: detailedBlog); }

question

Most helpful comment

To any one with the same issue ,you can consider the issue to be you bloc is still alive when routing to a new screen which indicates break some of it task into other blocs ,other properly popPush or pop and replace to close the bloc

All 4 comments

Hi @davidAg9 馃憢
Thanks for opening an issue!

Are you able to provide a link to a sample app which I can clone and run locally to reproduce the issue? Thanks! 馃憤

Fortunately you are a contributor since its a private repository

I think I was over using bloc although and since I wasn't popping any route in the above issue it kept the bloc listening for other states

To any one with the same issue ,you can consider the issue to be you bloc is still alive when routing to a new screen which indicates break some of it task into other blocs ,other properly popPush or pop and replace to close the bloc

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ricktotec picture ricktotec  路  3Comments

MahdiPishguy picture MahdiPishguy  路  3Comments

wheel1992 picture wheel1992  路  3Comments

craiglabenz picture craiglabenz  路  3Comments

rsnider19 picture rsnider19  路  3Comments