felangel / bloc

A predictable state management library that helps implement the BLoC design pattern
https://bloclibrary.dev
MIT License
11.86k stars 3.4k forks source link

question: Inquiry regarding removeListener in PostsList disposal process #4260

Closed sports7744 closed 1 month ago

sports7744 commented 1 month ago

Hi @felangel!

While working on a component inspired by the PostsList class from the Infinite list project, I noticed something that prompted a question. In the dispose method, there's an explicit call to removeListener for the _scrollController:

@override
void dispose() {
  _scrollController
    ..removeListener(_onScroll)
    ..dispose();
  super.dispose();
}

Could you provide some insight into why removeListener is used explicitly here? My understanding is that calling dispose() should typically take care of cleaning up listeners. I want to ensure that I'm following best practices, so any guidance on this decision would be greatly appreciated.

Thanks!

felangel commented 1 month ago

Hi @sports7744 👋 Thanks for opening an issue and good catch -- the call to removeListener is not necessary since the dispose call will remove the listeners. I fixed this in https://github.com/felangel/bloc/commit/41d232b0c5ed96050ed9fd2490c5a713b9a688a0, thanks again!