搜尋分類、套件與程式碼儲存庫

套件詳情v7.0.0

network_to_file_image

開啟本機影像檔案,若存在的話。若不存在,則從網路取得影像,並儲存至本機檔案,以便下次使用。

AndroidiOSmacOSWindowsLinux
近 30 天下載
1500
喜歡數
185
Pub 分數
140

安裝套件

flutter pub add network_to_file_image

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

一個 Flutter 套件,透過先檢查本機檔案來有效管理影像載入。若本機找不到影像,則從網路下載並儲存至磁碟以供未來使用,減少重複下載並提升效能。非常適合需要持久性影像快取且設定簡單的應用程式。

適用情境

  • 快取遠端影像以供離線存取
  • 減少重複的網路請求
  • 透過本機儲存改善應用程式效能
  • 顯示使用者個人檔案圖片並提供備用方案
  • 在列表視圖中載入影像並保持持久性

主要特色

  • 網路獲取前的本機檔案檢查
  • 下載後自動儲存圖片
  • 內建快取機制
  • 支援多個平台
  • 簡單 API 結合

適合對象

  • 需要離線圖像可用性的應用
  • 圖像密集型應用
  • 尋求輕量級快取的開發者
  • 後端基礎設施有限的專案
  • 針對行動和桌面平台的應用

使用建議

  • 在某些平台需要檔案系統權限
  • 磁碟空間使用量隨時間增加
  • 無內建的過期或清除策略
  • 可能需要手動快取管理
  • 對快取大小或TTL的控制有限

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

pub package

network_to_file_image

This package combines NetworkImage and FileImage to:

  • Download the image once from the URL,
  • Save it to the local file system,
  • Use the saved image from the file system from then on.

In more detail:

Given a file and url of an image, it first tries to read it from the local file. It decodes the given File object as an image, associating it with the given scale.

However, if the image doesn't yet exist as a local file, it fetches the given URL from the network, associating it with the given scale, and then saves it to the local file. The image will be cached regardless of cache headers from the server.

Notes:
  • If the provided url is null or empty, NetworkToFileImage will default to FileImage. It will read the image from the local file, and won't try to download it from the network.

  • If the provided file is null, NetworkToFileImage will default to NetworkImage. It will download the image from the network, and won't save it locally.

Use the package

Import path_provider in your pubspec.yaml file:

path_provider: ^2.1.1 

Then use it to create a File from a file name:

Future<File> file(String filename) async {
  Directory dir = await getApplicationDocumentsDirectory();
  String pathName = p.join(dir.path, filename);
  return File(pathName);
}

var myFile = await file("myFileName.png"),

Then, create the image:

Image(image: 
   NetworkToFileImage(
      url: "https://example.com/someFile.png", 
      file: myFile,
   )
)

If you make debug: true it prints to the console whether the image was read from the file or fetched from the network:

Image(image: 
   NetworkToFileImage(
      url: "https://example.com/someFile.png", 
      file: myFile, 
      debug: true,
   )
)    

Try running the NetworkToFileImage example.

Important:

The directory where you want to save the image must already exist in the local disk. Otherwise, the image won't be saved.

Canvas

You can also load images to use with the Canvas object of the paint method of a CustomPainter.

ImageProviders can't be used directly with the Canvas object, but you can use the provided ImageForCanvas class.

For example: Suppose a User object that contains url and filename properties:

var imageForCanvas = ImageForCanvas<User>(
        imageProviderSupplier: (User user) => NetworkToFileImage(file: user.file, url: user.url),
        keySupplier: (User user) => user.filename,
        loadCallback: (image, obj, key) => setState((){}),
      );

// While the image is downloading, this will return null.
var myImage = imageForCanvas.image(user);

if (myImage != null) {
    canvas.drawImage(myImage, ...);
    }

It will use the regular image cache from Flutter, and works not only with NetworkToFileImage provider, but any other image providers.

Try running the ImageForCanvas example.

Tests

You can set mock files (local and in the network). Please see methods:

  • setMockFile(File file, Uint8List bytes)
  • setMockUrl(String url, Uint8List bytes)
  • clearMocks()
  • clearMockFiles()
  • clearMockUrls()

Your mocked urls are usually only seen by the NetworkToFileImage class. However, you may override the default Dart http methods so that these urls are visible to other ImageProviders.

To that end, simply call this method:

NetworkToFileImage.startHttpOverride();

You can stop the http override by calling:

NetworkToFileImage.stopHttpOverride();

By Marcelo Glasberg

glasberg.dev
github.com/marcglasberg
linkedin.com/in/marcglasberg/
twitter.com/glasbergmarcelo
stackoverflow.com/users/3411681/marcg
medium.com/@marcglasberg

My article in the official Flutter documentation:

The Flutter packages I've authored:

My Medium Articles:

所屬分類

原始碼儲存庫

使用此套件的專案

暫無相關資料。