Bloc: Delegate error: Bad state: Cannot add new events after calling close

Created on 6 Feb 2020  路  5Comments  路  Source: felangel/bloc

Hi,

I am currently having an issue with Delegate error: Bad state: Cannot add new events after calling close and I'm not too sure how to deal with it.

Here is the description:

So I have set up an app with to display lessons and posts as the json below:

Lessons:
```
[
{
"url": "http://localhost:8000/lessons/1/",
"name": "First Lesson",
"release_date": "2020-02-04T00:00:00Z",
"posts": [
"http://localhost:8000/posts/8/",
"http://localhost:8000/posts/9/",
"http://localhost:8000/posts/10/"
]
},
{
"url": "http://localhost:8000/lessons/2/",
"name": "Lesson 2",
"release_date": "2020-02-05T21:29:25Z",
"posts": [
"http://localhost:8000/posts/12/",
"http://localhost:8000/posts/13/"
]
}
]


And I have 2 Blocs LessonBloc and PostBloc to help retrieve the respective jsons. 

On my homepage I have the following to help display the lessons: 

BlocBuilder(
builder: (context,state){
...
}else if(state is LessonsLoaded){
var length = state.lessons.length;
return ListView.builder(
itemCount: length,
itemBuilder: (context,index){
return GestureDetector(
onTap: () => Navigator.pushNamed(context, 'lesson',arguments:state.lessons[index]),
child: Tile('lib/images/lessons.jpg', state.lessons[index].name));
}
);
}else{
return Text("In Else");
}
},
),

and when the tiles are clicked on I basically want to pass the post urls to the next page where I actually retreive the posts. On the lesson details page I fetch the posts in the `initState()` function like so:

class LessonPage extends StatefulWidget {
Lesson lesson;
LessonPage({this.lesson});

@override
_LessonPageState createState() => _LessonPageState();
}

class _LessonPageState extends State {
PostsBloc _postsBloc;

@override
void initState() {
// TODO: implement initState
_refreshCompleter = Completer();
_postsBloc = BlocProvider.of(context)..add(FetchPosts(postUrlsList: widget.lesson.posts));
super.initState();
}

@override
void dispose() {
super.dispose();
_postsBloc.close();
print("closing post bloc");
}

The posts are displayed on the lessons page like: 

BlocBuilder(
builder: (context, state) {
...
} else if (state is PostsLoaded) {
return PageView.builder(
itemBuilder: (context, index) {
return Text(
state.posts[index].title,
),
} else {
return Center(
child: Text("loading..."),
);
}
},
)
```

This code works for the first time when I click on a lesson tile, but when I return to the homepage and click on a different lesson tile, I get the error `Delegate error: Bad state: Cannot add new events after calling close'.

I also tried to remove the _postsBloc.close(); from dispose() and the error goes away and the new post URLs get loaded BUT the builder fails to build the new posts, showing the old posts still.

I'm not too sure what I am doing wrong here, any advise is appreciated! And please let me know if I should provide more code. Thanks!

question

Most helpful comment

Ahh I see, I was getting the Close instances ofdart.core.Sink.dart(close_sinks) on dart lint but I just saw the other issue on that.

Thank you very much for your help!

All 5 comments

Hi @henryla92 馃憢
Thanks for opening an issue!

Are you able to share a link to the app or a sample app which illustrates the issue? Thanks!

Hi @henryla92 馃憢
Thanks for opening an issue!

Are you able to share a link to the app or a sample app which illustrates the issue? Thanks!

Of course, I just uploaded the app to https://github.com/henryla92/lessonsApp

Please let me know if you need anything else.

Thanks!

Just took a look and opened a pull request to address the issues. The problem was you were manually disposing blocs when you should just leave it to be handled by BlocProvider.

Hope that helps!

Ahh I see, I was getting the Close instances ofdart.core.Sink.dart(close_sinks) on dart lint but I just saw the other issue on that.

Thank you very much for your help!

No problem!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

frankrod picture frankrod  路  3Comments

nhwilly picture nhwilly  路  3Comments

shawnchan2014 picture shawnchan2014  路  3Comments

Reidond picture Reidond  路  3Comments

clicksocial picture clicksocial  路  3Comments