get方法獲取數(shù)據(jù),封裝存儲和移除方法用于操作數(shù)據(jù)緩存列表(需要優(yōu)化,僅參考)

我們提供的服務(wù)有:網(wǎng)站制作、成都網(wǎng)站設(shè)計、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認證、賀州ssl等。為成百上千家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的賀州網(wǎng)站制作公司
Flutter本地存儲可以用 shared_preferences ,其會根據(jù)不同操作系統(tǒng)進行相對應(yīng)的存儲。
在pubspec.yaml添加
`shared_preferences: ^2.0.13`
```d
import 'package:shared_preferences/shared_preferences.dart';
class SpUtils {
SharedPreferences?prefs;
SpUtils._() {
init();
}
static SpUtils?_instance;
static preInit() {
_instance ??=SpUtils._();
}
static SpUtilsgetInstance() {
_instance ??=SpUtils._();
return _instance!;
}
void init()async {
prefs ??=await SharedPreferences.getInstance();
}
setString(String key, String value) {
prefs!.setString(key, value);
}
setDouble(String key, double value) {
prefs!.setDouble(key, value);
}
setInt(String key, int value) {
prefs!.setInt(key, value);
}
setBool(String key, bool value) {
prefs!.setBool(key, value);
}
setStringList(String key, List value) {
prefs!.setStringList(key, value);
}
clear(String key){
prefs!.remove(key);
}
clearAll(){
prefs!.clear();
}
Tget(String key) {
return prefs!.get(key)as T;
}
}
```
在項目初始頁調(diào)用
`SpUtils.preInit();`
存
`SpUtils.getInstance().setString('userId', '12345678');`
`SpUtils.getInstance().setDouble('price', 12.88);`
`SpUtils.getInstance().setInt('count', 200);`
`SpUtils.getInstance().setBool('flag', true);`
取
`SpUtils.getInstance().get('userId');`
刪
`SpUtils.getInstance().clearAll();`
`SpUtils.getInstance().clear('userId');`
FadeInImage官方默認只支持緩存到內(nèi)存中,在項目中一般都需要把圖片緩存到本地文件中
通過觀察 FadeInImage 的構(gòu)造函數(shù)中,得知 image 是調(diào)用 ResizeImage.resizeIfNeeded(imageCacheWidth, imageCacheHeight, NetworkImage(image, scale: imageScale)) 這個方法來獲得圖片的,而獲得 ImageProvider 又是通過 NetworkImage(image, scale: imageScale)
繼續(xù)跟進發(fā)現(xiàn) NetworkImage 是繼承 ImageProvider 的一個抽象類,里面有個工廠構(gòu)造函數(shù)
通過修改這里的源碼來實現(xiàn)本地緩存圖片
在這之前需要先導(dǎo)入
Flutter的圖片緩存機制有問題(可能是我使用的版本1.12.13有問題)
網(wǎng)絡(luò)圖片會默認緩存到本地,但是不管圖片是不是完整的或者損壞的,導(dǎo)致頁面在下次進入的時候會優(yōu)先從緩存里讀取圖片。有些圖片是沒有加載完成的,或者損壞的,導(dǎo)致圖片無法顯示。UI效果就是顯示成白色的。
一種解決方式:加載前或者退出后清理圖片緩存
ImageCache??imageCache?=?PaintingBinding.instance.imageCache;?
imageCache.clear();
缺點就是每次圖片都想要從網(wǎng)絡(luò)上獲取,增加服務(wù)器負擔(dān)
在默認情況下頁面切換走時會被銷毀,頁面切換回來時會被重新創(chuàng)建,如果頁面中有列表那么整個列表將會被重新創(chuàng)建,降低了用戶體驗,下面是解決這個問題的幾種處理方式
按照給定尺寸進行圖片的解碼,而不是解碼整個圖片的尺寸,用來減少內(nèi)存的占用。
官方文檔:
官方說明:
Instructs Flutter to decode the image at the specified dimensions instead of at its native size.
This allows finer control of the size of the image in ImageCache and is generally used to reduce the memory footprint of ImageCache .
The decoded image may still be displayed at sizes other than the cached size provided here.
使用:
三方庫: cached_network_image 限2.5.0之后版本才可用
設(shè)定最大的緩存寬度和高度 this.maxWidthDiskCache 、 this.maxHeightDiskCache
使用:
從相冊選取圖片,展示時使用指定尺寸寬高進行處理。
使用三方庫:
使用自定義 provider 來指定所需圖片的寬高:
AssetEntityImageProvider 傳入寬高和圖片原圖 AssetEntity 數(shù)據(jù)。
provider 中 key.entity.thumbDataWithSize 方法:
進入 entity 中 thumbDataWithSize 方法:
進入 _getThumbDataWithId 方法中,
進入getThumb:
調(diào)用iOS原生的獲取圖片方法,
進入 getThumbWithId 方法,
原生實現(xiàn)獲取置頂寬高縮略圖方法實現(xiàn):
使用 iOS 原生類 PHImageManager 的
來獲取縮略圖。
網(wǎng)頁標(biāo)題:flutter緩存,flutter緩存數(shù)據(jù)
鏈接分享:http://chinadenli.net/article45/dsigpei.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計公司、虛擬主機、微信公眾號、網(wǎng)站內(nèi)鏈、搜索引擎優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)