未处理的异常:不受支持的操作:无法添加到固定长度列表,Hive

问题描述

您好,我使用 Hive 来存储我的应用程序数据,当我运行代码时,我可以添加主类别,并且可以为每个类别添加子类别。当我关闭应用程序并再次打开时,我可以添加主类别,但是当我尝试为现有的主类别添加子类别时,我无法添加新的子类别并且我遇到错误'未处理的异常:不支持的操作:无法添加到定长列表',怎么解决,卡住了,求大神帮帮忙

这是我的 categoryModel 和 subCategoryMoldel 类以及子类别屏幕。感谢您的帮助。

part 'categoryModel.g.dart';

@HiveType(typeId : 0)
class CategoryModel extends HiveObject{
  CategoryModel(
      { this.categoryId,this.categoryImagePath,this.categoryName,this.categoryColor,this.subCategoryModels});

  @HiveField(0)
  final int categoryColor;

  @HiveField(1)
  List <SubCategoryModel> subCategoryModels;

  @HiveField(2)
  int categoryId;

  @HiveField(3)
  String categoryImagePath;

  @HiveField(4)
  String categoryName;

}
// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'categoryModel.dart';

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

class CategoryModelAdapter extends TypeAdapter<CategoryModel> {
  @override
  final int typeId = 0;

  @override
  CategoryModel read(BinaryReader reader) {
    final numOfFields = reader.readByte();
    final fields = <int,dynamic>{
      for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),};
    return CategoryModel(
      categoryId: fields[2] as int,categoryImagePath: fields[3] as String,categoryName: fields[4] as String,categoryColor: fields[0] as int,subCategoryModels: (fields[1] as List)?.cast<SubCategoryModel>(),);
  }

  @override
  void write(BinaryWriter writer,CategoryModel obj) {
    writer
      ..writeByte(5)
      ..writeByte(0)
      ..write(obj.categoryColor)
      ..writeByte(1)
      ..write(obj.subCategoryModels)
      ..writeByte(2)
      ..write(obj.categoryId)
      ..writeByte(3)
      ..write(obj.categoryImagePath)
      ..writeByte(4)
      ..write(obj.categoryName);
  }

  @override
  int get hashCode => typeId.hashCode;

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

part 'subCategoryModel.g.dart';


@HiveType(typeId : 1)
class SubCategoryModel extends HiveObject{
  SubCategoryModel({
    this.subCategoryId,this.subCategoryCardId,this.subCategoryImagePath,this.subCategoryName,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;

  @HiveField(9)
  int subCategoryCardId;

}
// 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,};
    print(fields[3]);
    print(fields[0]);
    return SubCategoryModel(
      subCategoryId: fields[3] as int,subCategoryImagePath: fields[4] as String,subCategoryName: fields[5] as String,recipeId: fields[1] as double,ingredients: (fields[6] as List)?.cast<Ingredient>(),recipePhotoDir: fields[7] as String,recordedVoiceDir: fields[8] as String,subCategoryCardId:fields[9] as int,)..isCompleted = fields[2] as bool;
  }

  @override
  void write(BinaryWriter writer,SubCategoryModel obj) {
    writer
      ..writeByte(10)
      ..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)
      ..writeByte(9)
      ..write(obj.subCategoryCardId);
  }

  @override
  int get hashCode => typeId.hashCode;

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


class SubCategoriesScreen extends StatefulWidget {

  SubCategoriesScreen({this.categoryId,this.subCategoryImagePath});
  static String id="subCategoriesScreen";
  final String subCategoryName;
  final String subCategoryImagePath;
  final int categoryId;
  Widget buildBottomSheet(BuildContext context)=>AddMenuScreen(categoryId: categoryId,buttonText: "Tarif Ekle",route: "subCategoryScreen",);


  @override
  _SubCategoriesScreenState createState() => _SubCategoriesScreenState();
}

class _SubCategoriesScreenState extends State<SubCategoriesScreen> {

  int _flag=0;
  Box<CategoryModel> _categoryModelBox;
  CategoryModel _categoryModel;
  SubCategoryModel _subCategoryModel;

  void initState(){
    super.initState();
    _categoryModelBox = Hive.Box<CategoryModel>("categoryModelsInBox");
    _categoryModel = _categoryModelBox.getAt(widget.categoryId);

    print(_categoryModel);
    print(widget.subCategoryName);
    print(widget.subCategoryImagePath);
    print(widget.categoryId);


    if(widget.categoryId!=null && widget.subCategoryImagePath!=null && widget.subCategoryName!=null){
      addSubCategory();
      }
    }

    void addSubCategory()async{
       _subCategoryModel=SubCategoryModel(
          subCategoryCardId: _categoryModel.subCategoryModels.length,subCategoryId:widget.categoryId,subCategoryImagePath: widget.subCategoryImagePath,subCategoryName: widget.subCategoryName,categoryColor: Color((Random().nextDouble() * 0xFFFFFF).toInt()).withOpacity(0.5).value);

       print(_subCategoryModel.subCategoryImagePath);
       print(_subCategoryModel.subCategoryName);
       print(_subCategoryModel.subCategoryCardId);



       _categoryModel.subCategoryModels.add(_subCategoryModel);
       _categoryModel.save();
    }



  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        body: Scaffold(
              appBar: AppBar(
                automaticallyImplyLeading: false,centerTitle: true,title: BorderedText(
                  child:Text(
                    _categoryModel.categoryName,style: TextStyle(
                        color: Color(0XFFFFFB00),fontSize: 30,fontFamily: "OpenSans"
                    ),),strokeWidth: 5,strokeColor: Colors.black,elevation: 5,backgroundColor: Color(0xFFF2C3D4).withOpacity(1),leading: IconButton(
                  icon: Icon(Icons.arrow_back),onpressed: (){
                    Navigator.pop(context);
                  },iconSize: 40,color: Color(0xFFA2000B),actions: [
                  CircleAvatar(
                    radius: 27,backgroundColor: Colors.transparent,backgroundImage: Assetimage("images/cuttedlogo.PNG"),)
                ],body:Container(
                decoration: Boxdecoration(
                  image: decorationImage(
                    image: Assetimage("images/logoBGopacity.png"),fit: BoxFit.cover,child: Column(
                  crossAxisAlignment: CrossAxisAlignment.stretch,children: [
                    Expanded(
                      child: GridView.builder(
                          scrollDirection: Axis.vertical,gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2),itemCount:_categoryModel.subCategoryModels.length+1,itemBuilder: (context,index){
                            if(_categoryModel.subCategoryModels.length==0){
                              return EmptyCard(where: "subCategoryScreen",categoryId: _categoryModel.categoryId,);
                            }
                            if(_flag==1){
                              return EmptyCard(where: "subCategoryScreen",categoryId: _categoryModel.categoryId);
                            }
                            if(index==_categoryModel.subCategoryModels.length-1){
                              _flag=1;
                              return SubCategoryCard(subCategoryModel:_categoryModel.subCategoryModels[index]);
                            }
                            else{
                              return SubCategoryCard(subCategoryModel:_categoryModel.subCategoryModels[index]);
                            }
                          }
                      ),Column(
                      mainAxisAlignment: MainAxisAlignment.end,crossAxisAlignment: CrossAxisAlignment.stretch,children: [
                        Padding(
                          padding: EdgeInsets.all(10),child: Container(
                            decoration: Boxdecoration(
                              border: Border.all(style: BorderStyle.solid),color: kColorTheme7,borderRadius: BorderRadius.circular(40),child: TextButton(
                              onpressed: (){
                                showModalBottomSheet(
                                  isdismissible: false,enableDrag: false,context: context,builder: (BuildContext context)=> AddMenuScreen(categoryId:_categoryModel.categoryId,route:"subCategoryScreen"),);
                              },child: BorderedText(
                                strokeWidth: 5,child:Text("Tarif Ekle",style: TextStyle(
                                  color: Colors.white,fontFamily:'OpenSans',fontSize:30,],)
              ],)
      ),);
  }
} 

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)