我想在应用程序的localDB中存储一个图像,我使用Hive。但我找不到任何例子,我们可以在本地使用Uint8List与Hive存储文件。我可以用Hive保存原始数据类型,如String, int等,但我无法使用Hive存储任何东西,如图像等,尽管它承诺这样做。我找不到相同的例子。
我能够在我的应用程序中使用Hive在Flutter Web上完成缩略图,如下所示:
In My Hive Service
static const _thumbnailsKey = 'thumbnails';
static late Box<List<int>> _thumbnailsBox;
HiveService._();
static Future<void> initialize() async {
await Hive.initFlutter();
_thumbnailsBox = await Hive.openBox(_thumbnailsKey);
}
static Future<void> addThumbnailData({
required String key,
required List<int> data,
}) async {
await _thumbnailsBox.put(key, data);
}
static Uint8List getThumbnailData({required String key}) {
final bytes = _thumbnailsBox.get(key);
if (bytes == null) return Uint8List.fromList([]);
return Uint8List.fromList(bytes);
}
My Image Downloader Service
import 'dart:typed_data';
import 'package:http/http.dart' as http;
class ImageService {
ImageService._();
static Future<Uint8List> getImageData(String url) async {
Uri uri = Uri.parse(url);
final response = await http.get(uri);
return response.bodyBytes;
}
}
在我的小部件的ViewModel(或任何你喜欢的地方)
我使用GetX,所以我用_imageData更新我的图像小部件。Value =…
Future<void> _getThumbnailData() async {
final cachedThumbnailData = HiveService.getThumbnailData(key: 'someKey');
// This is where the magic happens
if (cachedThumbnailData.isNotEmpty) {
_imageData.value = cachedThumbnailData;
return;
}
// Otherwise fetch the image data
// In my case it's from a url
final imageData = await ImageService.getImageData(thumbnailUrl);
if (imageData.isEmpty) return;
// Update Hive for the next time you need the image
// You could also put this wherever you fetch your images
await HiveService.addThumbnailData(
key: reactionReportGroup.timestampNameKey,
data: imageData,
);
_imageData.value = imageData;
}
现在你可以使用MemoryImage(_viewModel.imageData)或Image.memory (_viewModel.imageData)
不要。将图像以文件形式保存在磁盘中,并将其名称存储在数据库中。
//Variables
List? image = [];
List? pickedImages;
List imagesAsBytes = [];//List of images converted to bytes
ImagesToSend imageObj = ImagesToSend(); //New instance of my HiveObject
//Picking images
Future pickImage() async {
setState(() {});
final pickedImages = await ImagePicker().pickMultiImage();
//You can change .pickMultiImage() to pickImage(source: ImageSource.camera) if you want it direct from the camera or pickImage(source: ImageSource.gallery) if you want from the the gallery
if (pickedImages == null) return;
image!.addAll(pickedImages);
setState(() => this.image = image);
//Saving in Hive
if (image == null) return;
imagesAsBytes = [];
for (var i in image!) {
File file = File(i.path);
imagesAsBytes.add(file.readAsBytesSync());
}
imageObj.images = imagesAsBytes;
boxImg.put('images_box', imageObj);
this.imagesAsBytes = boxImg.get('images_box').images;
}
//###############################################################
//My Model
//This is a Hive object that holds the images
//I have this as a different dart file, hopefully, you know what this is
@HiveType(typeId: 0)
class ImagesToSend extends HiveObject {
@HiveField(0)
List<Uint8List>? images;
}
//############################################################
//Don't forget to have the hive adapter and the box open on your main dart file
void main(List<String> args) async {
await Hive.initFlutter();
Hive.registerAdapter(ImagesToSendAdapter());
boxImg = await Hive.openBox<ImagesToSend>('images_box');
runApp(const MyApp());
}
这是我的解决方案,但我仍然面临问题。图像正在显示,但是,如果我关闭应用程序,蜂巢仍然会持有它们,但它不再显示它们了!如果你能想出这部分,让我知道…lol