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 #57

Closed
bw-flagship opened this issue Dec 4, 2024 · 5 comments
Closed

App does not start after removing a nested object #57

bw-flagship opened this issue Dec 4, 2024 · 5 comments

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 repo:
isar/hive#1321

@Rexios80
Copy link
Member

Rexios80 commented Dec 4, 2024

Use Hive.ignoreTypeId to solve this

@bw-flagship
Copy link
Author

@Rexios80 Thanks! This fixes it in my sample, so it seems to be the right approach. However, in my real world application, it does not work. Do you have any idea what I could try?

@Rexios80
Copy link
Member

Rexios80 commented Dec 5, 2024

I do not have any idea unless you are opening the box before making the ignore call

@bw-flagship
Copy link
Author

Okay, thanks. Than I'll close this issue since I can't provide a sample to reproduce

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

2 participants