feat: Implement image cache eviction for S3 images and enhance image loading error handling.
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:real_estate_mobile/core/network/api_client.dart';
|
||||
|
||||
/// Resolves S3 keys to presigned download URLs via the backend API.
|
||||
@@ -54,10 +53,16 @@ class ImageUrlResolver {
|
||||
return data['url'] as String?;
|
||||
}
|
||||
return null;
|
||||
} on DioException {
|
||||
} catch (_) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
void clearCache() => _cache.clear();
|
||||
|
||||
/// Evict a single key from the in-memory cache so the next resolve
|
||||
/// fetches a fresh presigned URL.
|
||||
void evict(String? key) {
|
||||
if (key != null) _cache.remove(key);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,13 +60,17 @@ class _S3ImageState extends State<S3Image> {
|
||||
return;
|
||||
}
|
||||
|
||||
final url = await ImageUrlResolver.instance.resolve(widget.imageUrl);
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_resolvedUrl = url;
|
||||
_loading = false;
|
||||
_failed = url == null;
|
||||
});
|
||||
try {
|
||||
final url = await ImageUrlResolver.instance.resolve(widget.imageUrl);
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_resolvedUrl = url;
|
||||
_loading = false;
|
||||
_failed = url == null;
|
||||
});
|
||||
}
|
||||
} catch (_) {
|
||||
if (mounted) setState(() { _loading = false; _failed = true; });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,12 +106,13 @@ class _S3ImageState extends State<S3Image> {
|
||||
|
||||
return CachedNetworkImage(
|
||||
imageUrl: _resolvedUrl!,
|
||||
cacheKey: widget.imageUrl,
|
||||
width: widget.width,
|
||||
height: widget.height,
|
||||
fit: widget.fit,
|
||||
alignment: widget.alignment,
|
||||
placeholder: (ctx, _) => _buildPlaceholder(ctx),
|
||||
errorWidget: (ctx, _, __) => _buildError(ctx),
|
||||
errorWidget: (ctx, _, _) => _buildError(ctx),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user