I have a large list with about 100+ items in a grid of 3, and they all have a cached_network_image in them.
Whenever I scroll the list a little fast, the images start to load. But if I keep scrolling, all the image requests are still active, so I'd reach the end of the list with 100+ image requests still active, even though those widgets are now disposed.
I tried contacting the developers of the package, and all they said was that yes, they do not cancel requests, which is not helpful at all.
Is there any way I can cancel those requests? Or at least sone way to save the app from suddenly crashing?
So, year 2025 and the problem still persists, and the solution is not that obvious. But here it is, if somebody encounters it:
/// First, you must create an instance of DisposableBuildContext
/// it's required by the ScrollAwareImageProvider
DisposableBuildContext? _disposableBuildContext;
@override
void initState() {
/// just wrap your widget's state in it
_disposableBuildContext = DisposableBuildContext(this);
}
... somewhere in your code use ScrollAwareImageProvider ->
return Container(
width: 500.0, /// replace with actual size of your image
height: 400.0,
decoration: BoxDecoration(
image: DecorationImage(
image: ScrollAwareImageProvider(
context: _disposableBuildContext!,
imageProvider: NetworkImage(
imageUrl,
),
),
fit: BoxFit.cover,
),
),
);
/// Then you MUST dispose _disposableBuildContext. That's it.
@override
void dispose() {
_disposableBuildContext?.dispose();
super.dispose();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With