flutter - NoSuchMethodError (NoSuchMethodError: 方法 'add' 在 null 上被调用
问题描述
我想用 JSON 文件中的一些数据填充 Flutter 中的列表。但是,我的代码不断抛出异常"NoSuchMethodError (NoSuchMethodError: The method 'add' was called on null."
我的错误在哪里?
JSON:
{
"#1": "6",
"#2": null,
"#3": null,
"#4": null,
"#5": null,
"#6": null,
"material_1": "stone",
"material_2": null,
"material_3": null,
"material_4": null,
"material_5": null,
"material_6": null,
}
我的代码:
List<String>getMaterialAmounts(){
List<String> materialAmountList;
for(int i = 0;i<6;i++){
materialAmountList.add(_json["#${i+1}"] ?? "-1");
}
return materialAmountList;
}
解决方案
你需要先初始化列表
改变
List<String> materialAmountList;
至
List<String> materialAmountList = new List();
推荐阅读
- python - 如何使用python替换excel文件中的单词
- react-native - React Native - 从另一个屏幕触发使用效果
- clickhouse - 如何检查 clickhouse 数据库服务器实时/活动连接
- uwp - 在 Microsoft 商店应用程序运行状况分析报告中将失败名称设为“未知”
- php - Text Past 不适用于创新的内容生成器
- ios - 将 UITextField heightAnchor 设置为 0 在 iOS 15 上不起作用
- javascript - 以选择特定列表项为条件进行文本输入
- r - 将data.frame转换为R中分隔字符串的函数的高性能实现
- r - Reading specific columns and rows from excel sheet in R
- javascript - 如何在 MAPBOX 中将过滤器的结果绘制成不同的颜色