当我实现我自己的方式把图像在Hive,但它没有工作。我在网上搜索,但没有找到任何合理的解决方案。一个完整的代码来实现它可能是有帮助的。任何帮助将不胜感激。谢谢你,谢谢
我用来挑选图像并将其放入Hive的代码。
File? _image;
Future getAndSaveImage() async {
final image = await ImagePicker().pickImage(source: ImageSource.gallery);
if (image == null) return;
final tempImage = File(image.path);
setState(() {
_image = tempImage;
images.put(_nameController.text, ProfileImage(_image!));
});
}
配置单元模型类
import 'dart:io';
import 'package:hive/hive.dart';
part 'profile_image.g.dart';
@HiveType(typeId: 2)
class ProfileImage {
ProfileImage(this.profileStudentImage);
@HiveField(0)
final File profileStudentImage;
}
请给予一个适当的工作解决方案,我真的需要它。😢
1条答案
按热度按时间8i9zcol21#
您首先需要将图像转换为可以存储在Hive中的格式,通常是Uint8List(字节数组)。然后,您可以将此字节数组保存到Hive框中
在将图像保存到Hive之前,您需要将其转换为Uint8List。您可以使用ImagePicker包选择图像并将其转换为字节。
保存图像到配置单元:
一旦你有了Uint8List格式的图像,你可以保存它到Hive:
图片来自Hive:
显示图像: