我想从本地手机存储加载图像,显示它并保存在缓存中,以便我可以在其他页面上重新加载它:这是我尝试做的:
IconButton(
icon: Icon(
Icons.camera_alt,
color: Colors.white,
),
onPressed: () async {
File image =
await ImagePicker.pickImage(
source: ImageSource.gallery);
var directory =
await getApplicationDocumentsDirectory();
final String path = directory.path;
final String fileName = pathPackage.basename(image.path);
final File localImage = await image.copy('$path/$fileName');
print'chemin de limage :${localImage.path}');
isImageLoaded = true;
await RestClient.persistStringDataInStorage'pp', localImage.path);
},
))
方法 persistStringDataInStorage
定义如下:
static Future<String> persistStringDataInStorage(
String key, String data) async {
return SharedPreferences.getInstance().then((prefs) {
if (prefs.getString(key) != data) {
prefs.setString(key, data);
} else {
prefs.remove(key);
prefs.setString(key, data);
}
return 'success';
}).catchError((error) {
print(error);
return 'error';
});
}`
然后我尝试将存储的路径分配给一个字符串变量并显示在这样的小部件中:
BoxDecoration(
shape: BoxShape.circle,
image: new DecorationImage(
image: (photo != '')
? new ExactAssetImage(photo)
: new ExactAssetImage(
'assets/as.png'),
但有时图像不会出现,我可以在其他地方重新加载。