我在下面分享小部件,我面临来自
的错误 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),)),
),
),
);
},
),
),
创建成分对象后,我尝试添加对象列表。 这是对象模型的代码。
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;
}
/AndroidAudioRecorder( 5385): handleHasPermission true
E/flutter ( 5385): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: NoSuchMethodError: The method 'add' was called on null.
E/flutter ( 5385): Receiver: null
E/flutter ( 5385): Tried calling: add(Instance of 'Ingredient')
E/flutter ( 5385): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5)
E/flutter ( 5385): #1 _AddIngredientsCardState.build.<anonymous closure>.<anonymous closure> (package:lezzet_kitabi/recipe_screen_cards.dart:382:124)
E/flutter ( 5385): #2 _AddIngredientsCardState.build.<anonymous closure>.<anonymous closure> (package:lezzet_kitabi/recipe_screen_cards.dart:379:34)
E/flutter ( 5385): #3 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:182:24)
E/flutter ( 5385): #4 TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:607:11)
E/flutter ( 5385): #5 BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:296:5)
E/flutter ( 5385): #6 BaseTapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:267:7)
E/flutter ( 5385): #7 GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:157:27)
E/flutter ( 5385): #8 GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:443:20)
E/flutter ( 5385): #9 GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:419:22)
E/flutter ( 5385): #10 RendererBinding.dispatchEvent (package:flutter/src/rendering/binding.dart:288:11)
E/flutter ( 5385): #11 GestureBinding._handlePointerEventImmediately (package:flutter/src/gestures/binding.dart:374:7)
E/flutter ( 5385): #12 GestureBinding.handlePointerEvent (package:flutter/src/gestures/binding.dart:338:5)
E/flutter ( 5385): #13 GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:296:7)
E/flutter ( 5385): #14 GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:279:7)
E/flutter ( 5385): #15 _rootRunUnary (dart:async/zone.dart:1370:13)
E/flutter ( 5385): #16 _CustomZone.runUnary (dart:async/zone.dart:1265:19)
E/flutter ( 5385): #17 _CustomZone.runUnaryGuarded (dart:async/zone.dart:1170:7)
E/flutter ( 5385): #18 _invoke1 (dart:ui/hooks.dart:186:10)
E/flutter ( 5385): #19 PlatformDispatcher._dispatchPointerDataPacket (dart:ui/platform_dispatcher.dart:282:7)
E/flutter ( 5385): #20 _dispatchPointerDataPacket (dart:ui/hooks.dart:96:31)
请帮助我如何解决它? 祝你今天过得愉快.............................................. ………………………………………………………………………………………………………………………………………………………… .....................................
答案 0 :(得分:1)
这一行导致错误:
categoryModels[widget.subCategoryId].subCategoryModels[widget.subCategoryCardId].ingredients.add(ingredient);
因为 categoryModels[widget.subCategoryId].subCategoryModels[widget.subCategoryCardId].ingredients
是 null
,您可以尝试先检查它是否为空,例如添加 ?
:
categoryModels[widget.subCategoryId].subCategoryModels[widget.subCategoryCardId]?.ingredients