Baseflow / flutter_cached_network_image

Download, cache and show images in a flutter app
https://baseflow.com
2.4k stars 631 forks source link

When an image not found error occurs, the image error is not cached when using memCacheHeight and memCacheWidth #928

Open olaripihlak opened 3 months ago

olaripihlak commented 3 months ago

🔙 Regression

In case of image error, error is not cached when using memCacheHeight and memCacheWidth.

Started with Flutter version 3.16 and also reproducible in 3.19. Works correctly with Flutter 3.13.

Old (and correct) behavior

Image error is cached and network call is only made once when using memCacheHeight and memCacheWidth.

Current behavior

Image error is not cached and network call is made every time when using memCacheHeight and memCacheWidth.

Reproduction steps

import 'dart:async';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';

void main() {
  runApp(MaterialApp(
    home: Scaffold(
      appBar: AppBar(title: Text('Reload Image')),
      body: const MyImageWidget(),
    ),
  ));
}

class MyImageWidget extends StatefulWidget {
  const MyImageWidget({super.key});

  @override
  MyImageWidgetState createState() => MyImageWidgetState();
}

class MyImageWidgetState extends State<MyImageWidget> {
  static const imageCacheKey = 'one_day_cached_network_image_key';

  static CacheManager cacheManagerInstance = CacheManager(
    Config(
      imageCacheKey,
      stalePeriod: const Duration(days: 1),
    ),
  );

  Timer? _timer;

  @override
  void initState() {
    super.initState();
    _startTimer();
  }

  @override
  void dispose() {
    _timer?.cancel();
    super.dispose();
  }

  void _startTimer() {
    const duration = Duration(seconds: 5);
    _timer = Timer.periodic(duration, (timer) {
      if (mounted) {
        setState(() {});
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return CachedNetworkImage(
      imageUrl: "https://random/icon.png",
      memCacheHeight: 100,
      memCacheWidth: 100,
      imageBuilder: (context, imageProvider) => Image(image: imageProvider),
      errorWidget: (context, url, error) => const Icon(Icons.nearby_error_outlined),
    );
  }
}

Configuration

Version: 3.3.1

Platform: