簡體   English   中英

如何在使用路徑提供程序時覆蓋或更新 Flutter 中的緩存文件

[英]How to Overwrite or Update Cache files in Flutter while using Path Provider

下面的代碼是我使用路徑提供程序為本地緩存創建的

String fileName="pathString.json";
var dir=await getTemporaryDirectory();
File file=File(dir.path+"/"+fileName);

if(!file.existsSync()) {
        final http.Response response = await http.get(
            Uri.parse("https://*************.herokuapp.com/*********"));
        file.writeAsStringSync(
            response.body, flush: true, mode: FileMode.write);
        final data = file.readAsStringSync();
        responseData = json.decode(data);
      }else{
        final data = file.readAsStringSync();
        responseData = json.decode(data);
        final http.Response response = await http.get(
            Uri.parse("https://**************.herokuapp.com/*********"));
        file.writeAsStringSync(
            response.body, flush: true, mode: FileMode.write);
      }

第一次,可以創建文件。 但是第二次.. 一旦文件被創建,並且 API 獲取帶有更新數據的最新響應,緩存文件就不會被覆蓋。

如果有人可以幫助解決這個問題,那就太好了..

提前致謝。

我想在你的情況下發生了與我的情況類似的事情:

對於 flutter Image class 我發現,它使用ImageCache緩存圖像,因此當您重寫圖像並將其保存在文件系統中時,圖像看起來是一樣的,直到您重新啟動應用程序或使緩存無效。

我為圖像緩存失效編寫的代碼:

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

// imgBytes - raw image bytes here empty just for example
// location - path to image in fs
updateImage(Uint8List imgBytes) async {
  var file = File(location);
  await file.writeAsBytes(imgBytes, mode: FileMode.write); // rewrite file content

  var img = Image.file(file); // create Image class
  img.image.evict(); // invalidate cache key for this image
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM