Taming Flutter Infinite Scroll: Why 3 Lines of async* Missed the Point, and How BlocSignal Fixes It
The Ubiquitous Infinite Scroll Pagination Bug Almost every Flutter engineer has encountered the dreaded infinite scroll race condition in production. The user opens a list, flings their thumb down the screen on a spotty cellular connection, and triggers multiple scroll notifications past the bottom threshold within milliseconds. Before the first asynchronous HTTP network request finishes, the scroll listener fires again. Suddenly, your list duplicates items, page counters jump ahead, or the state machine locks up entirely. Recently, mobile developer Ali Wajdan published a widely discussed article titled 3 Lines of Dart async* Code That Fixed My Infinite Scroll Pagination . In his article, Ali accurately diagnoses the root cause of standard pagination headaches: "Most Flutter pagination code I have seen, including my own for years, wraps a mutable state object around a scroll listener. A page counter, a loading boolean, a hasMore flag, and a fetch method the UI calls when it hits the scroll threshold. It works until two scroll events fire close together, or a rebuild triggers a second load before the first future resolves... It is a classic race condition, and it gets worse once the state lives across a page counter, a hasMore flag, and a loading flag that all need to stay in sync." To escape this trap, Ali suggested encapsulating pagination logic inside a Dart async* generator and consuming it with a StreamIterator : // The pattern proposed in Ali Wajdan's article Stream < List < Post >> fetchPostsPaginated ( String query ) async * { var page = 0 ; var hasMore = true ; while ( hasMore ) { final batch = await api . fetchPosts ( query , page: page ); hasMore = batch . isNotEmpty ; page ++ ; yield batch ; } } final iterator = StreamIterator ( fetchPostsPaginated ( query )); Future < List < Post >> loadNextPage () async { if ( ! await iterator . moveNext ()) return const []; return iterator . current ; } On the surface, moving mutable state into local generator variable