【发布时间】:2020-07-31 09:09:28
【问题描述】:
我对这种情况有意见。你能帮助我吗 ?我正在接收此错误消息。
发生异常。
NoSuchMethodError (NoSuchMethodError: 方法 'addItem' 在 null 上被调用。 接收方:空 尝试调用:addItem("{\"name\":\"example\",\"isCompleted\":false,\"isArchived\":false}"))
我在这里使用 addItem;
floatingActionButton: FloatingActionButton(
backgroundColor: Color(0xff655c56),
onPressed: () async {
String itemName = await showDialog(
context: context,
builder: (BuildContext context) => ItemDialog());
if (itemName.isNotEmpty) {
var item =
Item(name: itemName, isCompleted: false, isArchived: false);
_itemService.addItem(item.toJson());
setState(() {});
}
},
我在这里定义了addItem;
Future<List<Item>> fetchItems() async {
final response = await http.get(_serviceUrl);
if (response.statusCode == 200) {
Iterable items = json.decode(response.body);
return items.map((item) => Item.fromJson(item)).toList();
} else {
throw Exception('something went wrong');
}
}
Future<Item> addItem(String itemJson) async{
final response = await http.post(_serviceUrl, headers: {
'content-type':'application/json'
},body: itemJson);
if(response.statusCode==201){
Map item= json.decode(response.body);
return Item.fromJson(item);
}
else {
throw Exception('something went wrong');
}
}
}
帮助! 顺便更新一下
【问题讨论】:
-
请发布您的完整代码
标签: list flutter mobile methods