I'm setting up Hive in my flutter flame game and getting typeId 33 is not found error. I tried deleting Hive from the directory, flutter project cache clean & rebuild, redownloading and upgrading all pacakges. I've tried all solutions on the internet, but it still does not work. I think I'm getting this error when getting a box... because it says it cannot find the adapter.
here are the code files -->
plyaer_data.dart file:
import 'package:hive/hive.dart';
part 'player_data.g.dart';
@HiveType(typeId: 0)
class PlayerData extends HiveObject {
@HiveField(0)
int health = 0;
@HiveField(1)
int itemsCollected = 0;
@HiveField(2)
int totalItemsNum = 0;
@HiveField(3)
bool isOkToNextFloor = false;
@HiveField(4)
bool showControls = false;
@HiveField(5)
bool isSoundEffectOn = true;
@HiveField(6)
bool isMusicOn = true;
@HiveField(7)
double soundEffectVolume = 1.0;
@HiveField(8)
double musicVolume = 1.0;
@HiveField(9)
int currentFloorIndex = 0;
}
player_data.g.dart file:
part of 'player_data.dart';
// **************************************************************************
// TypeAdapterGenerator
// **************************************************************************
class PlayerDataAdapter extends TypeAdapter<PlayerData> {
@override
final int typeId = 0;
@override
PlayerData read(BinaryReader reader) {
final numOfFields = reader.readByte();
final fields = <int, dynamic>{
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
};
return PlayerData()
..health = fields[0] as int
..itemsCollected = fields[1] as int
..totalItemsNum = fields[2] as int
..isOkToNextFloor = fields[3] as bool
..showControls = fields[4] as bool
..isSoundEffectOn = fields[5] as bool
..isMusicOn = fields[6] as bool
..soundEffectVolume = fields[7] as double
..musicVolume = fields[8] as double
..currentFloorIndex = fields[9] as int;
}
@override
void write(BinaryWriter writer, PlayerData obj) {
writer
..writeByte(10)
..writeByte(0)
..write(obj.health)
..writeByte(1)
..write(obj.itemsCollected)
..writeByte(2)
..write(obj.totalItemsNum)
..writeByte(3)
..write(obj.isOkToNextFloor)
..writeByte(4)
..write(obj.showControls)
..writeByte(5)
..write(obj.isSoundEffectOn)
..writeByte(6)
..write(obj.isMusicOn)
..writeByte(7)
..write(obj.soundEffectVolume)
..writeByte(8)
..write(obj.musicVolume)
..writeByte(9)
..write(obj.currentFloorIndex);
}
@override
int get hashCode => typeId.hashCode;
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is PlayerDataAdapter &&
runtimeType == other.runtimeType &&
typeId == other.typeId;
}
main.dart file:
PlayerData playerData = PlayerData();
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Flame.device.fullScreen();
await Flame.device.setLandscape();
runApp(const MyApp());
if (playerData.isMusicOn) {
FlameAudio.bgm.play('main-menu-music.mp3', volume: playerData.musicVolume);
}
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
themeMode: ThemeMode.dark,
darkTheme: ThemeData.dark().copyWith(
textTheme: GoogleFonts.pressStart2pTextTheme(),
scaffoldBackgroundColor: Colors.black,
),
home: FutureBuilder<PlayerData>(
initialData: PlayerData(),
future: getGameData(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const CircularProgressIndicator();
} else if (snapshot.hasError) {
return Text('Error: ${snapshot.error}');
} else {
return const MainMenu();
}
},
),
);
}
}
Future<void> initHive() async {
final dir = await getApplicationDocumentsDirectory();
await Hive.initFlutter(dir.path);
Hive.registerAdapter<PlayerData>(PlayerDataAdapter());
}
Future<PlayerData> getGameData() async {
await initHive();
final box = await Hive.openBox<PlayerData>('PlayerDataBox');
final gameData = box.get('PlayerData');
if (gameData == null) {
box.put('PlayerData', PlayerData());
}
return box.get('PlayerData')!;
}