gpt4 book ai didi

image - 如何在 flutter 中将图像存储到缓存的网络图像

转载 作者:行者123 更新时间:2023-12-05 09:34:38 24 4
gpt4 key购买 nike

我有以下代码,我从 firebase 存储中获取图像作为图像。现在,我想将此图像存储在我的 CachedNetworkImage 中,这样我就不必每次都从数据库中获取它。由于 cachednetworkimage 需要一个 URL 而我正在获取一个图像,我该如何使用 cachednetworkimage?

这是我的代码;


final FirebaseStorage storage = FirebaseStorage(
app: Firestore.instance.app,
storageBucket: 'gs://my-project.appspot.com');

Uint8List imageBytes;
String errorMsg;

_MyHomePageState() {
storage.ref().child('selfies/me2.jpg').getData(10000000).then((data) =>
setState(() {
imageBytes = data;
})
).catchError((e) =>
setState(() {
errorMsg = e.error;
})
);
}

@override
Widget build(BuildContext context) {
var img = imageBytes != null ? Image.memory(
imageBytes,
fit: BoxFit.cover,
) : Text(errorMsg != null ? errorMsg : "Loading...");

return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
),
body: new ListView(
children: <Widget>[
img,
],
));
}
}```

最佳答案

缓存网络图像和 Flutter 缓存管理器

Cached network image 包依赖于另一个名为 Flutter 缓存管理器的包来存储和检索图像文件。

Flutter cache manager

您需要下载您的图像文件并使用包将它们放入缓存中。下面是一个示例代码,它从 Firebase 存储中获取文件及其下载 URL 并将它们放入缓存中:

// import the flutter_cache_manager package
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
// ... other imports

class MyCacheManager {
Future<void> cacheImage() async {
final FirebaseStorage storage = FirebaseStorage(
app: Firestore.instance.app,
storageBucket: 'gs://my-project.appspot.com',
);

final Reference ref = storage.ref().child('selfies/me2.jpg');

// Get your image url
final imageUrl = await ref.getDownloadURL();

// Download your image data
final imageBytes = await ref.getData(10000000);

// Put the image file in the cache
await DefaultCacheManager().putFile(
imageUrl,
imageBytes,
fileExtension: "jpg",
);
}
}

Cached network image

接下来,您将使用 CacheNetworkImage 小部件,如 documentation 中所示.

// ... some code

@override
Widget build(BuildContext context) {
return Scaffold(
body: CachedNetworkImage(
imageUrl: "your_image_link_here",
placeholder: (context, url) => CircularProgressIndicator(),
errorWidget: (context, url, error) => Icon(Icons.error),
),
);
}

如果您使用 Flutter 缓存管理器将图像文件放入缓存中,缓存网络图像应该直接从缓存中检索它们。如果您的图像文件过期或缓存以某种方式被清除,它将为您下载并将它们放入缓存中。

完整示例

import 'package:cached_network_image/cached_network_image.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:flutter/material.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';

class MyCacheManager {
final _storage = FirebaseStorage(
app: FirebaseFirestore.instance.app,
storageBucket: 'gs://my-project.appspot.com',
);

final defaultCacheManager = DefaultCacheManager();

Future<String> cacheImage(String imagePath) async {
final Reference ref = _storage.ref().child(imagePath);

// Get your image url
final imageUrl = await ref.getDownloadURL();

// Check if the image file is not in the cache
if ((await defaultCacheManager.getFileFromCache(imageUrl))?.file == null) {
// Download your image data
final imageBytes = await ref.getData(10000000);

// Put the image file in the cache
await defaultCacheManager.putFile(
imageUrl,
imageBytes,
fileExtension: "jpg",
);
}

// Return image download url
return imageUrl;
}
}

class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
String _imageUrl;

@override
void initState() {
final myCacheManager = MyCacheManager();

// Image path from Firebase Storage
var imagePath = 'selfies/me2.jpg';

// This will try to find image in the cache first
// If it can't find anything, it will download it from Firabase storage
myCacheManager.cacheImage(imagePath).then((String imageUrl) {
setState(() {
// Get image url
_imageUrl = imageUrl;
});
});

super.initState();
}

@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: _imageUrl != null
? CachedNetworkImage(
imageUrl: _imageUrl,
placeholder: (context, url) => CircularProgressIndicator(),
errorWidget: (context, url, error) => Icon(Icons.error),
)
: CircularProgressIndicator(),
),
);
}
}

关于image - 如何在 flutter 中将图像存储到缓存的网络图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66488125/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com