Top Related Projects
SVG parsing, rendering, and widget library for Flutter
Download, cache and show images in a flutter app
Quick Overview
Flutter Cached Network Image is a Flutter package that provides an easy way to load and cache network images. It extends the functionality of Flutter's Image widget by adding caching capabilities, placeholder support, and error handling for network images.
Pros
- Efficient caching mechanism for improved performance
- Supports placeholder images while loading
- Provides error handling for failed image loads
- Customizable caching options and behaviors
Cons
- May increase app size due to additional dependencies
- Potential memory usage concerns with large image caches
- Limited control over low-level caching details
- Occasional issues with image refresh when the source changes
Code Examples
Loading a simple cached network image:
CachedNetworkImage(
imageUrl: "https://example.com/image.jpg",
)
Using a placeholder and error widget:
CachedNetworkImage(
imageUrl: "https://example.com/image.jpg",
placeholder: (context, url) => CircularProgressIndicator(),
errorWidget: (context, url, error) => Icon(Icons.error),
)
Customizing cache behavior:
CachedNetworkImage(
imageUrl: "https://example.com/image.jpg",
cacheManager: CacheManager(
Config(
"customCacheKey",
stalePeriod: Duration(days: 7),
maxNrOfCacheObjects: 100,
),
),
)
Getting Started
- Add the dependency to your
pubspec.yaml
file:
dependencies:
cached_network_image: ^3.2.3
- Import the package in your Dart file:
import 'package:cached_network_image/cached_network_image.dart';
- Use the
CachedNetworkImage
widget in your Flutter app:
CachedNetworkImage(
imageUrl: "https://example.com/image.jpg",
placeholder: (context, url) => CircularProgressIndicator(),
errorWidget: (context, url, error) => Icon(Icons.error),
)
Competitor Comparisons
SVG parsing, rendering, and widget library for Flutter
Pros of flutter_svg
- Specialized for rendering SVG files in Flutter applications
- Supports a wide range of SVG features and elements
- Allows for easy scaling and manipulation of vector graphics
Cons of flutter_svg
- Limited to SVG files, not suitable for other image formats
- May have performance issues with complex SVG files
- Doesn't provide built-in caching mechanisms for network SVGs
Code Comparison
flutter_svg:
SvgPicture.network(
'https://example.com/image.svg',
height: 200,
width: 200,
placeholderBuilder: (BuildContext context) => CircularProgressIndicator(),
)
flutter_cached_network_image:
CachedNetworkImage(
imageUrl: "https://example.com/image.jpg",
placeholder: (context, url) => CircularProgressIndicator(),
errorWidget: (context, url, error) => Icon(Icons.error),
)
Key Differences
- Image format support: flutter_svg is specific to SVG files, while flutter_cached_network_image supports various image formats.
- Caching: flutter_cached_network_image provides built-in caching for network images, which flutter_svg lacks.
- Vector graphics: flutter_svg excels in rendering and manipulating vector graphics, while flutter_cached_network_image is primarily for raster images.
- Performance: flutter_cached_network_image may have better performance for large images due to its caching mechanism, while flutter_svg might struggle with complex SVGs.
Choose flutter_svg for vector graphics and SVG-specific needs, and flutter_cached_network_image for general image loading with caching capabilities.
Download, cache and show images in a flutter app
Pros of flutter_cached_network_image
- Identical functionality and features as both repositories are the same
- Well-maintained and actively developed
- Widely used in the Flutter community
Cons of flutter_cached_network_image
- No significant cons as both repositories are identical
- Potential confusion due to duplicate repositories
Code Comparison
Both repositories contain the same code, so there are no differences to highlight. Here's a sample of the main CachedNetworkImage
widget from both repositories:
class CachedNetworkImage extends StatefulWidget {
/// Creates a widget that displays a [placeholder] while an [imageUrl] is loading,
/// then cross-fades to display the loaded image.
const CachedNetworkImage({
Key? key,
required this.imageUrl,
this.httpHeaders,
this.imageBuilder,
this.placeholder,
this.progressIndicatorBuilder,
this.errorWidget,
// ... (additional parameters)
}) : super(key: key);
// ... (rest of the class implementation)
}
Both repositories contain identical code, making them functionally equivalent. The existence of two identical repositories might be due to a naming or organization issue within the Baseflow GitHub account. Users can choose either repository without any functional differences.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Cached network image
A flutter library to show images from the internet and keep them in the cache directory.
Sponsors
Try the Flutter Chat Tutorial ð¬
How to use
The CachedNetworkImage can be used directly or through the ImageProvider. Both the CachedNetworkImage as CachedNetworkImageProvider have minimal support for web. It currently doesn't include caching.
With a placeholder:
CachedNetworkImage(
imageUrl: "http://via.placeholder.com/350x150",
placeholder: (context, url) => CircularProgressIndicator(),
errorWidget: (context, url, error) => Icon(Icons.error),
),
Or with a progress indicator:
CachedNetworkImage(
imageUrl: "http://via.placeholder.com/350x150",
progressIndicatorBuilder: (context, url, downloadProgress) =>
CircularProgressIndicator(value: downloadProgress.progress),
errorWidget: (context, url, error) => Icon(Icons.error),
),
Image(image: CachedNetworkImageProvider(url))
When you want to have both the placeholder functionality and want to get the imageprovider to use in another widget you can provide an imageBuilder:
CachedNetworkImage(
imageUrl: "http://via.placeholder.com/200x150",
imageBuilder: (context, imageProvider) => Container(
decoration: BoxDecoration(
image: DecorationImage(
image: imageProvider,
fit: BoxFit.cover,
colorFilter:
ColorFilter.mode(Colors.red, BlendMode.colorBurn)),
),
),
placeholder: (context, url) => CircularProgressIndicator(),
errorWidget: (context, url, error) => Icon(Icons.error),
),
How it works
The cached network images stores and retrieves files using the flutter_cache_manager.
FAQ
My app crashes when the image loading failed. (I know, this is not really a question.)
Does it really crash though? The debugger might pause, as the Dart VM doesn't recognize it as a caught exception; the console might print errors; even your crash reporting tool might report it (I know, that really sucks). However, does it really crash? Probably everything is just running fine. If you really get an app crashes you are fine to report an issue, but do that with a small example so we can reproduce that crash.
See for example this or this answer on previous posted issues.
Top Related Projects
SVG parsing, rendering, and widget library for Flutter
Download, cache and show images in a flutter app
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot