Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

App does not start after removing a nested object #1321

Open
bw-flagship opened this issue Dec 4, 2024 · 1 comment
Open

App does not start after removing a nested object #1321

bw-flagship opened this issue Dec 4, 2024 · 1 comment

Comments

@bw-flagship
Copy link

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]
@bw-flagship
Copy link
Author

Mirrored in hive_CE:
IO-Design-Team/hive_ce#57

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant