OptimizedArtworkData class
An optimized version of ArtworkData with advanced memory management features.
OptimizedArtworkData extends the basic ArtworkData functionality with:
- Automatic caching with memory pressure handling
- Streaming support for very large images
- Compression for memory efficiency
- Smart loading strategies based on image size
- Performance monitoring and metrics
This class is designed for applications that need to handle large numbers of artwork images efficiently while minimizing memory usage.
Features
- Smart Loading: Automatically chooses between lazy, streaming, or cached loading
- Memory Optimization: Uses compression and caching to minimize memory footprint
- Size-Based Strategy: Different handling for small, medium, and large artwork
- Performance Tracking: Monitors loading times and cache effectiveness
- Backward Compatibility: Drop-in replacement for standard ArtworkData
Usage Examples
Basic Usage (Drop-in Replacement)
final optimizedArtwork = OptimizedArtworkData(
mimeType: 'image/jpeg',
type: ArtworkType.frontCover,
description: 'Album cover',
dataLoader: () => loadImageBytes(),
);
// Works exactly like ArtworkData
final imageBytes = await optimizedArtwork.data;
With Custom Cache
final cache = ArtworkCache(maxMemoryBytes: 100 * 1024 * 1024);
final artwork = OptimizedArtworkData(
mimeType: 'image/png',
type: ArtworkType.frontCover,
dataLoader: () => loadImageBytes(),
cache: cache,
cacheKey: 'album_123_cover',
);
Streaming Large Images
final largeArtwork = OptimizedArtworkData(
mimeType: 'image/jpeg',
type: ArtworkType.frontCover,
dataLoader: () => loadLargeImageBytes(),
enableStreaming: true,
streamingThreshold: 5 * 1024 * 1024, // 5MB
);
// Process in chunks for large images
if (largeArtwork.shouldUseStreaming) {
await for (final chunk in largeArtwork.streamData()) {
processImageChunk(chunk);
}
}
Memory Management Strategy
The class uses a tiered approach based on image size:
- Small images (< 100KB): Direct loading, no caching overhead
- Medium images (100KB - 5MB): Lazy loading with optional caching
- Large images (> 5MB): Streaming with minimal memory footprint
Performance Considerations
- Cache lookups add minimal overhead for cache hits
- Compression is only applied when beneficial (> 10% savings)
- Streaming adds processing overhead but eliminates memory spikes
- Size thresholds can be tuned based on application requirements
- Implemented types
Constructors
-
OptimizedArtworkData({required String mimeType, required ArtworkType type, String? description, required Future<
Uint8List> dataLoader(), ArtworkCache? cache, String? cacheKey, bool enableStreaming = true, int streamingThreshold = 5 * 1024 * 1024, int compressionThreshold = 512 * 1024, LazyArtworkLoader? lazyLoader}) - Creates a new OptimizedArtworkData instance.
- OptimizedArtworkData.fromArtworkData(ArtworkData artworkData, {ArtworkCache? cache, String? cacheKey, bool enableStreaming = true, int streamingThreshold = 5 * 1024 * 1024, int compressionThreshold = 512 * 1024})
-
Creates an OptimizedArtworkData from a standard ArtworkData.
factory
- OptimizedArtworkData.fromLazyLoader(LazyArtworkLoader loader, {required String mimeType, required ArtworkType type, String? description, ArtworkCache? cache, String? cacheKey, bool enableStreaming = true, int streamingThreshold = 5 * 1024 * 1024, int compressionThreshold = 512 * 1024})
-
Creates an OptimizedArtworkData from a LazyArtworkLoader.
factory
Properties
-
data
→ Future<
Uint8List> -
Gets the actual image data using the optimal loading strategy.
no setteroverride
- description → String?
-
Optional human-readable description of the artwork.
no setteroverride
- estimatedSize → int?
-
Gets the estimated size of the artwork data in bytes.
no setter
- fileExtension → String?
-
Returns the typical file extension for this artwork's MIME type.
no setteroverride
- formatDescription → String
-
Returns a human-readable description of this artwork's format.
no setteroverride
- hashCode → int
-
The hash code for this object.
no setterinherited
- hasImmediateData → bool
-
Whether image data is available without async loading.
no setteroverride
- immediateData → Uint8List?
-
Gets immediately available image data for synchronous operations.
no setteroverride
- isLossy → bool
-
Returns true if this artwork's MIME type uses lossy compression.
no setteroverride
- isVector → bool
-
Returns true if this artwork's MIME type is vector-based.
no setteroverride
- mimeType → String
-
The MIME type of the artwork image.
no setteroverride
- mimeTypeEnum → MimeType?
-
Returns the MimeType enum value for this artwork's MIME type.
no setteroverride
- performanceMetrics → ArtworkPerformanceMetrics
-
Gets performance metrics for this artwork instance.
no setter
-
props
→ List<
Object?> -
The list of properties that will be used to determine whether
two instances are equal.
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- shouldUseStreaming → bool
-
Determines if this artwork should use streaming based on its size.
no setter
- stringify → bool?
-
If set to
true, thetoStringmethod will be overridden to output this instance'sprops.no setterinherited - supportsTransparency → bool
-
Returns true if this artwork's MIME type supports transparency.
no setteroverride
- type → ArtworkType
-
The type/category of this artwork image.
no setteroverride
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
streamData(
{int chunkSize = 64 * 1024}) → Stream< Uint8List> ? - Creates a stream for processing large artwork data in chunks.
-
streamDataWithProgress(
{int chunkSize = 64 * 1024}) → Stream< StreamingChunk> ? - Creates a stream with progress information for large artwork.
-
toString(
) → String - A string representation of this object.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited