8

I have tried all of the following Widget to load images from network :

  • Image.network()
  • CachedNetworkImage()

And also their ImageProvider :

  • NetworkImage
  • CachedNetworkImageProvider

There is no bool to choose not to cache images. The only way i have found is to load the ImageProvider like in initState() and then call evict() right after.

I don't really know if this works actually or if this is the best way to do...

Is there any way to prevent caching from network "natively" ?

Tom3652
  • 2,540
  • 3
  • 19
  • 45
  • Image.network() does not cache the image. it downloads it again every time u go to widget. – Benyamin Aug 14 '21 at 15:29
  • from the documentation https://api.flutter.dev/flutter/widgets/Image/Image.network.html : ```All network images are cached regardless of HTTP headers.```. Am i not understanding this sentence correctly ? – Tom3652 Aug 14 '21 at 15:32
  • I guess you are right. reference here https://stackoverflow.com/questions/47209606/how-do-i-clear-flutters-image-cache I think this is your answer – Benyamin Aug 14 '21 at 15:36
  • thank you for the link, but does it mean that every instance of ImageCache will be cleared automatically ? Meaning that everytime we load an ImageProvider it will be evicted from cache ? – Tom3652 Aug 14 '21 at 15:40
  • 1
    Where are you testing/ web does cache automatically. But on android/ios it doesn't – Md. Yeasin Sheikh Aug 14 '21 at 15:49
  • Yes it is Android / IOS only. and you are right i have the output that there is no "pending" cache configuration for a given provider. – Tom3652 Aug 14 '21 at 15:54

2 Answers2

16

You can achieve that simply by adding ? followed by timestamp at the end of your image url.

Image.network(
   '${imgUrl}?${DateTime.now().millisecondsSinceEpoch.toString()}',
   fit: BoxFit.cover,
),
luke77
  • 2,255
  • 2
  • 18
  • 30
  • 4
    darn! so clever! – Ruelos Joel Jul 08 '22 at 08:33
  • 2
    It works because the URL is unique, but every url is cached anyway, you just can't fall back on the old one. My goal was to not put anything in cache so i can't accept this answer although it's useful ! – Tom3652 Aug 17 '22 at 07:41
7

I have just created a widget that gets the image as a URL and then download it as Uint8List and show it in a Image.memory widget.

You can use it like this:

NonCacheNetworkImage('https://riverpod.dev/img/logo.png'),
import 'dart:typed_data';

import 'package:flutter/material.dart';
import 'package:http/http.dart';

class NonCacheNetworkImage extends StatelessWidget {
  const NonCacheNetworkImage(this.imageUrl, {Key? key}) : super(key: key);
  final String imageUrl;
  Future<Uint8List> getImageBytes() async {
    Response response = await get(Uri.parse(imageUrl));
    return response.bodyBytes;
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder<Uint8List>(
      future: getImageBytes(),
      builder: (context, snapshot) {
        if (snapshot.hasData) return Image.memory(snapshot.data!);
        return SizedBox(
          width: 100,
          height: 100,
          child: Text("NO DATA"),
        );
      },
    );
  }
}
Sajad Abdollahi
  • 1,593
  • 1
  • 7
  • 19
  • Are you sure Image.memory does not cache ? I mean I kept retrying using your method then cecking ( imageCache.currentSize ) & ( imageCache.liveImageCount ) and still gave me some values,, and need to know for certain that Image.memory does not cache to be able to check other places for my problem – Rageh Azzazy Nov 02 '22 at 04:57