我正在尝试实现一个从服务器删除特定数据(基于id)的函数。
我的功能看起来像这样:
Future<bool> deleteData(int index) {
_data.removeAt(index);
notifyListeners();
return http
.delete(
'https://*my address*/${_data[index].id}.json')
.then((http.Response response) {
return true;
}).catchError((error) {
print(error);
return false;
});
}
数据将在本地删除,但不会在服务器上删除。相反,我得到这个错误:
I / flutter(5517):══╡通过手套拍摄的异常╞══════════════════════════════════════════════════════════════════════════════════════════════════ ═══════════════════════════════════════════════════════════════════════════════════════════════════════════════ / flutter(5517):RangeError(index):无效值:有效值范围为空:0
我找到了解决问题的方法 -
Future<bool> deleteData(int index) {
selectedDataId = _data[index].id //new line
_data.removeAt(index);
notifyListeners();
return http
.delete(
'https://*my address*/${selectedDataId}.json')
.then((http.Response response) {
return true;
}).catchError((error) {
print(error);
return false;
});
}
因为我在本地删除了我的数据,我需要在var之前存储它然后使用这个var