How to save a List<Object> and retrieve using Hive?
- sewookori
- 2022년 9월 30일
- 1분 분량
Step 1: Add those dependencies on pubspec.yaml file
dependencies:
hive: ^2.2.3
hive_flutter: ^1.1.0
dev_dependencies:
hive_generator: ^1.1.3
build_runner: ^[latest version]
Step 2: Make the class like this
part 'wallpaper.g.dart';
@HiveType(typeId: 0, adapterName: "WallpaperAdapter")
class Wallpaper{
@HiveField(0)
final String date;
@HiveField(1)
final String url;
Wallpaper({required this.date, required this.url});
}
Step 3: Run the command on terminal
1. flutter packages pub run build_runner build
or
2. flutter packages pub run build_runner watch --delete-conflicting-outputs
Step 4: Now register the adapter on main.dart
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Hive.initFlutter();
Hive.registerAdapter(WallpaperAdapter());
await Hive.openBox('myBox');
runApp(MyApp());
}
Step 5: Now restart your application and let's enjoy 😊


댓글