4

i'm new in flutter i want to know how to get path from cached images network in flutter or how to save network image to asset image and get the path of that image?

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
Dimas N AL
  • 135
  • 2
  • 6
  • Welcome to Stack Overflow! Please [edit] to add meaningful code and a problem description here. What have you tried so far? Posting a [Minimal, Complete, Verifiable Example](http://stackoverflow.com/help/mcve) that demonstrates your problem would help you get better answers. Thanks! – Tim Diekmann May 22 '18 at 15:27

3 Answers3

12

The newer way to do this is using DefaultCacheManager from the Flutter Cache Manager package.

final cache = DefaultCacheManager(); // Gives a Singleton instance
final file = await cache.getSingleFile(yourURL);
// Now use file.path
sjsam
  • 21,411
  • 5
  • 55
  • 102
  • `DefaultCacheManager()` does not return a Future. So it should read `final cache = DefaultCacheManager();`. Otherwise you get `'await' applied to 'DefaultCacheManager', which is not a 'Future'.` – Burnash Nov 05 '21 at 19:04
  • It must be `var file = await DefaultCacheManager().getSingleFile(url);` – Alireza Beitari Nov 06 '21 at 18:15
  • 1
    @Burnash : That is correct. Thanks for pointing it out. From [here](https://raw.githubusercontent.com/Baseflow/flutter_cache_manager/develop/flutter_cache_manager/lib/src/cache_managers/default_cache_manager.dart), we can also confirm that `DefaultCacheManager()` gives a singleton instance. – sjsam Nov 07 '21 at 02:04
9

cached_network_image uses the flutter_cache_manager under the hood to save images locally (code here).

To find the path you need to access the file through the cache manager.

import 'package:flutter_cache_manager/flutter_cache_manager.dart';

Future<String> _findPath(String imageUrl) async {
  final cache = await CacheManager.getInstance();
  final file = await cache.getFile(imageUrl);
  return file.path;
}
Edman
  • 5,335
  • 29
  • 32
  • thanks for your answer, but i still have some problem how to make network image to be asset? – Dimas N AL May 23 '18 at 06:24
  • Can you share why you need it in assets? My understanding is that assets are packaged with the app at compile time, as implied [here](https://flutter.io/assets-and-images/). However, [here](https://docs.flutter.io/flutter/services/AssetBundle-class.html) they do mention it "could be loaded from the file system", but don't say how. I think they mean you can implement your own `AssetBundle` and set that as the `DefaultAssetBundle`. – Edman May 23 '18 at 07:03
  • i need it for make an wallpaper app with using network image from api, and use this similar code [here](https://stackoverflow.com/questions/44181343/how-do-i-share-an-image-on-ios-and-android-using-flutter) and in that code using asset image to share the image between flutter and native java with platform channel, so i think i should use asset image to send my network image, and i don't know how to set asset bundle with DefaultAssetBundle (using network image) code?, thanks for your answer – Dimas N AL May 24 '18 at 03:00
  • In that example the assets are the original source of the image but that doesn't really matter for the rest of the operation. In `_shareImage` they save the image from assets into the app directory, get a `File` reference to it, and then call platform code with the file name. You should be able to do the same, except that you retrieve the file name from `_findPath`. – Edman May 24 '18 at 06:32
  • so how to make an network image to be file image? so that i can get a file and then call it, thank you – Dimas N AL May 30 '18 at 00:16
0

All Cache is stored in one particular place for that app. Any will get trigger their cache for further use. Use flutter_cache_manager to find out data.

Future<String> _cachePath(String cacheUrl) async {
  final cache = await CacheManager.getInstance();
  var file = await cache.getFile(imageUrl);
  return file.path;
}
Vishal_VE
  • 1,852
  • 1
  • 6
  • 9