IO-Design-Team / hive_ce

Hive Community Edition - A spiritual continuation of Hive v2
https://pub.dev/packages/hive_ce
Apache License 2.0
51 stars 3 forks source link

App does not start after removing a nested object #57

Open bw-flagship opened 11 hours ago

bw-flagship commented 11 hours ago

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

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

bw-flagship commented 11 hours ago

Mirrored in hive repo: https://github.com/isar/hive/issues/1321

Rexios80 commented 6 hours ago

Use Hive.ignoreTypeId to solve this