您必须使用 @HiveType() 注释您的对象。并且必须注册您的对象 Hive.registerAdapter(WallpaperAdapter(), 0);。
然而,您是否必须part 'wallpaper.g.dart';
生成所需的代码?
编辑:首先在您的 pubspec 上导入依赖项:
dependencies:
hive: ^[version]
hive_flutter: ^[version]
dev_dependencies:
hive_generator: ^[version]
build_runner: ^[version]
Hive.registerAdapter(MyObjectAdapter(), 0);
你应该把你的功能main.dart
。在 runApp 之前
你的 HiveObject 应该有这样的注释:
@HiveType()
class Person extends HiveObject {
@HiveField(0);
String name;
@HiveField(1);
int age;
}
将此命令放在您的导入附近part 'person.g.dart';
并在您的终端上运行代码生成。flutter packages pub run build_runner build
.
带有代码生成的 Hive 功能,因此此命令将生成您需要的文件