0

您好,我使用 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.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;

  @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, dynamic>{
      for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
    };
    print(fields[3]);
    print(fields[0]);
    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,
      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.subCategoryName, 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,buttonText: "Tarif Ekle", route:"subCategoryScreen"),
                                );
                              },
                              child: BorderedText(
                                strokeWidth: 5,
                                strokeColor: Colors.black,
                                child:Text("Tarif Ekle",style: TextStyle(
                                  color: Colors.white,
                                  fontFamily:'OpenSans',
                                  fontSize:30,
                              ),
                            ),
                          ),
                        ),
                      ),
                    ),
                  ],
                )
              ],
            ),
          ),
        )
      ),
    );
  }
} 

4

0 回答 0