hive
hive copied to clipboard
App does not start after removing a nested object
Description When removing a nested object in hive that was filled with data before, the app does not start any longer. I found it in my production app first and could reproduce it in a minimal sample.
Steps to Reproduce
- Use Code sample for first Run
- run flutter pub run build_runner build
- run the app
- Use Code sample for second Run
- run flutter pub run build_runner build
- run the app
Actual result HiveError: Cannot read, unknown typeId: 34. Did you forget to register an adapter? (App does not start)
Expected result App starts and is usable
Code sample for first Run
import 'package:flutter/material.dart';
import 'package:hive_flutter/hive_flutter.dart';
part 'main.g.dart';
void main() async {
await Hive.initFlutter();
Hive.registerAdapter(MyClassAdapter());
Hive.registerAdapter(MyClass2Adapter());
final box = await Hive.openBox<MyClass>("boxname");
await box.clear();
await box.add(
MyClass()
..a = 1
..b =MyClass2()..a=7
..c = 3,
);
print("count: ${box.values.length}");
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: 'Flutter Demo',
home: Scaffold(),
);
}
}
@HiveType(typeId: 1)
class MyClass {
@HiveField(0)
int? a;
@HiveField(1)
MyClass2? b;
@HiveField(2)
int? c;
}
@HiveType(typeId: 2)
class MyClass2 {
@HiveField(0)
int? a;
}
Code sample for second Run
import 'package:flutter/material.dart';
import 'package:hive_flutter/hive_flutter.dart';
part 'main.g.dart';
void main() async {
await Hive.initFlutter();
Hive.registerAdapter(MyClassAdapter());
// Hive.registerAdapter(MyClass2Adapter());
final box = await Hive.openBox<MyClass>("boxname");
await box.clear();
// await box.add(
// MyClass()
// ..a = 1
// ..b =MyClass2()..a=7
// ..c = 3,
// );
print("count: ${box.values.length}");
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: 'Flutter Demo',
home: Scaffold(),
);
}
}
@HiveType(typeId: 1)
class MyClass {
@HiveField(0)
int? a;
// @HiveField(1)
// MyClass2? b;
@HiveField(2)
int? c;
}
// @HiveType(typeId: 2)
// class MyClass2 {
// @HiveField(0)
// int? a;
// }
Version
- Platform: Android
- Flutter version: [3.22]
- Hive version: [2.2.3]
Mirrored in hive_CE: https://github.com/IO-Design-Team/hive_ce/issues/57