我该如何解决 getter 'length' 被调用为 null接收者:空尝试调用:长度?

问题描述

我有一个屏幕显示主类别的子类别,我使用 hive 来保存和接收我的应用程序数据,但是当我使用未来的构建器返回指定的对象时,我遇到了标题错误, 我分享页面和我的课程请帮助我谢谢...................................................... ....................


class _SubCategoriesScreenState extends State<SubCategoriesScreen> {
  int _flag=0;

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

    void addSubCategory()async{
      SubCategoryModel subCategoryModel=SubCategoryModel(
          subCategoryId:widget.categoryId,subCategoryImagePath: widget.subCategoryImagePath,subCategoryName: widget.subCategoryName,categoryColor: Color((Random().nextDouble() * 0xFFFFFF).toInt()).withOpacity(0.5).value);
      if(categoryModels!=null){
        for (var categoryModel in categoryModels){
          if (categoryModel.categoryId == subCategoryModel.subCategoryId){
            categoryModel.subCategoryModels.insert(categoryModel.subCategoryModels.length-1,subCategoryModel);
            categoryModel=await hiveHelper.getCategoryModel(widget.categoryId);
            categoryModel.subCategoryModels.add(subCategoryModel);
            hiveHelper.addCategoryModel(categoryModel);
          }
        }
      }
    }



  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: FutureBuilder(
        future: HiveHelper().getCategoryModel(widget.categoryId),builder: (BuildContext context,AsyncSnapshot<CategoryModel> snapshot) {
          var _categoryModel=snapshot.data;
          Future.delayed( Duration(milliseconds: 900));
          return  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:snapshot.data.subCategoryModels.length+1,itemBuilder: (context,index){
                              if(_categoryModel.subCategoryModels.length==0){
                                return EmptyCard(where: "subCategoryScreen",categoryId: widget.categoryId,);
                              }
                              if(_flag==1){
                                return EmptyCard(where: "homeScreen",);
                              }
                              if(index==_categoryModel.subCategoryModels.length-1){
                                _flag=1;
                                SubCategoryModel subCategoryModel = snapshot.data.subCategoryModels[index];
                                return SubCategoryCard(subCategoryCardId:index,subCategoryId:subCategoryModel.subCategoryId,subcategoryName: subCategoryModel.subCategoryName,subCategoryImagePath:subCategoryModel.subCategoryImagePath,subCategoryCardColor: subCategoryModel.categoryColor,);
                              }
                              else{
                                SubCategoryModel subCategoryModel =  snapshot.data.subCategoryModels[index];
                                return SubCategoryCard(subCategoryCardId: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:widget.categoryId,buttonText: "Tarif Ekle",route:"subCategoryScreen"),);
                                },child: BorderedText(
                                  strokeWidth: 5,child:Text("Tarif Ekle",style: TextStyle(
                                    color: Colors.white,fontFamily:'OpenSans',fontSize:30,],)
                ],);
        },);
  }
}

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;

  @HiveField(5)
  SubCategoryModel subCategoryModel;
}
part 'subCategoryModel.g.dart';


@HiveType(typeId : 1)
class SubCategoryModel extends HiveObject{
  SubCategoryModel({
    this.subCategoryId,this.subCategoryImagePath,this.subCategoryName,this.recipeId,List<Ingredient>ingredients,this.recipePhotoDir,this.recordedVoiceDir,bool isCompeted});

  @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;

}

解决方法

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

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

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