1

我使用 hive 作为我的应用程序的数据库,我创建了我的类的适配器,但是当我尝试运行我的应用程序时,我在下面出现异常...................... ..................................................... ..................................................... .....................................................

E/flutter (14408): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: type 'String' is not a subtype of type 'int' in type cast
E/flutter (14408): #0      SubCategoryModelAdapter.read (package:lezzet_kitabi/subCategoryModel.g.dart:20:32)
E/flutter (14408): #1      BinaryReaderImpl.read (package:hive/src/binary/binary_reader_impl.dart:326:33)
E/flutter (14408): #2      BinaryReaderImpl.readFrame (package:hive/src/binary/binary_reader_impl.dart:274:26)
E/flutter (14408): #3      FrameHelper.framesFromBytes (package:hive/src/binary/frame_helper.dart:21:26)
E/flutter (14408): #4      FrameIoHelper.framesFromFile (package:hive/src/io/frame_io_helper.dart:42:12)
E/flutter (14408): <asynchronous suspension>
E/flutter (14408): #5      StorageBackendVm.initialize (package:hive/src/backend/vm/storage_backend_vm.dart:86:11)
E/flutter (14408): <asynchronous suspension>
E/flutter (14408): #6      HiveImpl._openBox (package:hive/src/hive_impl.dart:113:9)
E/flutter (14408): <asynchronous suspension>
E/flutter (14408): #7      HiveImpl.openBox (package:hive/src/hive_impl.dart:142:12)
E/flutter (14408): <asynchronous suspension>
E/flutter (14408): #8      main (package:lezzet_kitabi/main.dart:28:3)
E/flutter (14408): <asynchronous suspension>
E/flutter (14408): 

我共享类及其适配器

part 'subCategoryModel.g.dart';


@HiveType(typeId : 1)
class SubCategoryModel extends HiveObject{
  SubCategoryModel({
    this.subCategoryId,
    this.subCategoryImagePath,
    this.subCategoryName,
    this.categoryColor,
    this.recipeId,
    this.ingredients,
    this.recipePhotoDir,
    this.recordedVoiceDir});

  @HiveField(0)
  final int categoryColor;

  @HiveField(1)
  final double recipeId;

  @HiveField(2)
  bool isCompleted;

  @HiveField(3)
  int subCategoryId;

  @HiveField(4)
  String subCategoryImagePath;

  @HiveField(5)
  String subCategoryName;

  @HiveField(6)
  List <Ingredient> ingredients;

  @HiveField(7)
  String recipePhotoDir;

  @HiveField(8)
  String recordedVoiceDir;

}

// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'subCategoryModel.dart';

// **************************************************************************
// TypeAdapterGenerator
// **************************************************************************

class SubCategoryModelAdapter extends TypeAdapter<SubCategoryModel> {
  @override
  final int typeId = 1;

  @override
  SubCategoryModel read(BinaryReader reader) {
    final numOfFields = reader.readByte();
    final fields = <int, dynamic>{
      for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
    };
    return SubCategoryModel(
      subCategoryId: fields[3] as int,
      subCategoryImagePath: fields[4] as String,
      subCategoryName: fields[5] as String,
      categoryColor: fields[0] as int,
      recipeId: fields[1] as double,
      ingredients: (fields[6] as List)?.cast<Ingredient>(),
      recipePhotoDir: fields[7] as String,
      recordedVoiceDir: fields[8] as String,
    )..isCompleted = fields[2] as bool;
  }

  @override
  void write(BinaryWriter writer, SubCategoryModel obj) {
    writer
      ..writeByte(9)
      ..writeByte(0)
      ..write(obj.categoryColor)
      ..writeByte(1)
      ..write(obj.recipeId)
      ..writeByte(2)
      ..write(obj.isCompleted)
      ..writeByte(3)
      ..write(obj.subCategoryId)
      ..writeByte(4)
      ..write(obj.subCategoryImagePath)
      ..writeByte(5)
      ..write(obj.subCategoryName)
      ..writeByte(6)
      ..write(obj.ingredients)
      ..writeByte(7)
      ..write(obj.recipePhotoDir)
      ..writeByte(8)
      ..write(obj.recordedVoiceDir);
  }

  @override
  int get hashCode => typeId.hashCode;

  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
      other is SubCategoryModelAdapter &&
          runtimeType == other.runtimeType &&
          typeId == other.typeId;
}

你能帮我吗 谢谢。

4

0 回答 0