Flutter 未处理的异常:NoSuchMethodError:方法';添加';被调用为空。?
我在下面分享我面临错误的小部件Flutter 未处理的异常:NoSuchMethodError:方法';添加';被调用为空。?,flutter,Flutter,我在下面分享我面临错误的小部件 Expanded( flex: 40, child: ListView.builder( shrinkWrap: true, itemCount: items.length, itemBuilder: (context, index) {
Expanded(
flex: 40,
child: ListView.builder(
shrinkWrap: true,
itemCount: items.length,
itemBuilder: (context, index) {
return Padding(
padding: EdgeInsets.all(2.0),
child: GestureDetector(
onTap: ()async{
if(_ingredients.length==0){
Ingredient ingredient= Ingredient(ingredientName:"${items[index]}",dropDownValue: "Çay Kaşığı",ingredientAmount: null);
categoryModels[widget.subCategoryId].subCategoryModels[widget.subCategoryCardId].ingredients.add(ingredient);
addIngredient(ingredient,context);
_toggleCardHeight();
_toggleCardSize();
setState(() {
});
}
else{
var flag=0;
for(var i in _ingredients){
if(i.ingredientName==items[index]){
flag=-1;
var response= await showAlertDialog(context);
if (response==false){
setState(() {});
}
}
}
if(flag==0){
Ingredient ingredient=Ingredient(ingredientName:"${items[index]}",dropDownValue: "Çay Kaşığı",ingredientAmount: null);
categoryModels[widget.subCategoryId].subCategoryModels[widget.subCategoryCardId].ingredients.add(ingredient);
addIngredient(ingredient,context);
setState(() {});
}
}
},
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(90),
border: Border.all(style:BorderStyle.solid,width: 1),
color: Colors.white54,
),
child: Padding(
padding: EdgeInsets.all(5),
child: Text('${items[index]}',style: TextStyle(fontWeight: FontWeight.bold),)),
),
),
);
},
),
),
在我创建了Components objet之后,我正在尝试添加一个对象列表。
下面是对象模型的代码
class CategoryModel{
CategoryModel(
{
this.categoryId,
this.categoryImagePath,
this.categoryName,
this.categoryColor,
this.subCategory
});
final SubCategoryModel subCategory;
final Color categoryColor;
final List<SubCategoryModel> subCategoryModels=[SubCategoryModel()];
int categoryId;
String categoryImagePath;
String categoryName;
}
class SubCategoryModel{
SubCategoryModel(
{
this.subCategoryId,
this.subCategoryImagePath,
this.subCategoryName,
this.categoryColor,
this.recipeId,
this.ingredients,
this.recipePhotoDir,
this.recipeTextFromSpeechProcessing,
this.recordedVoiceDir});
final Color categoryColor;
final double recipeId;
int subCategoryId;
String subCategoryImagePath;
String subCategoryName;
List<Ingredient> ingredients=[];
String recipeTextFromSpeechProcessing;
String recipePhotoDir;
String recordedVoiceDir;
}
类分类模型{
分类模型(
{
这个,分类号,
此.categoryImagePath,
这个.categoryName,
这个,分类颜色,
这是一个子类别
});
最终子类别模型子类别;
最终颜色类别颜色;
最终列表子类别模型=[SubCategory Model()];
int分类;
字符串类别图像路径;
字符串类别名称;
}
类子类别模型{
子类别模型(
{
此.subcategory ID,
this.subcategory图像路径,
此.subcategory名称,
这个,分类颜色,
这是我的,
这是我的配料,
这个,recipePhotoDir,
此.recipeTextFromSpeechProcessing,
此参数为.recordedVoiceDir});
最终颜色类别颜色;
最终的双倒数;
int子类别id;
字符串子类别ImagePath;
字符串子类别名称;
列出成分=[];
字符串recipeTextFromSpeechProcessing;
字符串recipePhotoDir;
字符串记录VoiceDir;
}
错误
/AndroidAudioRecorder(5385):handleHasPermission true
E/flatter(5385):[ERROR:flatter/lib/ui/ui\u dart\u state.cc(186)]未处理的异常:NoSuchMethodError:对null调用了方法“add”。
E/颤振(5385):接收器:空
E/flatter(5385):尝试调用:add(“配料”实例)
E/颤振(5385):#0 Object.noSuchMethod(飞镖:核心补丁/Object_补丁。飞镖:54:5)
E/颤振(5385):#1#额外的危险状态。构建。。(包装:lezzet_kitabi/配方_screen_cards.省道:382:124)
E/颤振(5385):#2#额外的危险状态。构建。。(包装:lezzet_kitabi/配方_screen_cards.省道:379:34)
E/flatter(5385):#3手势识别器.invokeCallback(包:flatter/src/signatures/recognizer.dart:182:24)
E/flatter(5385):#4 TapGestureRecognitor.handleTapUp(软件包:flatter/src/signatures/tap.dart:607:11)
电子/颤振(5385):#5个基本手势识别器。#检查(包装:颤振/src/手势/tap.省道:296:5)
E/flatter(5385):#6 BaseTapgestureRecognitor.AcceptPirse(包:flatter/src/pirses/tap.dart:267:7)
E/flatter(5385):#7个手势arenamanager.sweep(包装:flatter/src/signatures/arena.dart:157:27)
E/flatter(5385):#8 GestureBinding.handleEvent(包装:flatter/src/signatures/binding.dart:443:20)
E/flatter(5385):#9 GestureBinding.dispatchEvent(包:flatter/src/signatures/binding.dart:419:22)
E/flatter(5385):#10 renderbinding.dispatchEvent(包:flatter/src/rendering/binding.dart:288:11)
E/颤振(5385):#11手势绑定。#立即使用手点(包装:颤振/src/手势/绑定。省道:374:7)
E/flatter(5385):#12 GestureBinding.handlePointerEvent(包:flatter/src/signatures/binding.dart:338:5)
E/颤振(5385):#13个手势绑定。(u flushpointerreeventqueue(包装:颤振/src/手势/绑定。省道:296:7)
E/flatter(5385):#14手势绑定。"handlepointerdata包(包:flatter/src/signatures/binding.dart:279:7)
E/颤振(5385):#15#根运行单位(dart:async/zone.dart:1370:13)
E/flatter(5385):#16 _CustomZone.runUnary(dart:async/zone.dart:1265:19)
E/flatter(5385):#17 _CustomZone.runUnaryGuarded(dart:async/zone.dart:1170:7)
E/颤振(5385):#18_调用1(省道:ui/钩子。省道:186:10)
E/flatter(5385):#19平台调度程序。"调度点数据包(dart:ui/平台调度程序。dart:282:7)
E/flatter(5385):#20 dispatchPointerDataPacket(dart:ui/hooks.dart:96:31)
请帮助我如何解决它?
祝您有一个愉快的一天。…这一行导致了错误:
categoryModels[widget.subCategoryId].subCategoryModels[widget.subCategoryCardId].ingredients.add(ingredient);
因为categoryModels[widget.subCategoryId].subCategoryModels[widget.subCategoryCardId].Components
为null
您可以尝试先检查它是否为null,就像添加?
:
categoryModels[widget.subCategoryId].subCategoryModels[widget.subCategoryCardId]?.ingredients
好的,谢谢,但是为什么它是空的,这个语句上面的创建对象仍然面临同样的问题。为什么它是空的取决于你在其中使用的方法,我的意思是你编写的逻辑假设你正在使用一个条件,如果它是真的,我们会返回空,但我不能确定我是否没有看到完整的代码并测试它,但是,我建议您将项目迁移到空安全模式,以便更好地处理空错误。
categoryModels[widget.subCategoryId].subCategoryModels[widget.subCategoryCardId]?.ingredients