我想從庫存串列中洗掉一個物件,其中我只有庫存的描述和 url,我想通過描述洗掉庫存物件,那么我該如何洗掉該物件。
服務類中的功能
這是我的服務班
Future<dynamic> requestToRemoveInventory(
String accessToken, List<Inventory> list) async {
try {
var response = await http.patch(Uri.parse(AppUrl.removeInventory),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
'Authorization': 'Bearer $accessToken'
},
body: jsonEncode({"inventory": list}));
if (response.statusCode == 200 || response.statusCode == 201) {
var responseJson = jsonDecode(response.body);
return responseJson;
} else {
var responseJson = jsonDecode(response.body);
print(responseJson);
}
} on SocketException {
throw NoInternetException('No Internet Service');
}
}
這是我的控制器類
deleteInventory(List<Inventory> list, BuildContext context) async {
String? accessToken = await preferenceService.getAccessToken();
inventoryService.requestToRemoveInventory(accessToken!, list).then((value) {
getMyInvenoryFromService();
}).catchError((error) {
showSnackBar(error.toString(), context);
});
}
請告訴我我必須寫什么邏輯才能洗掉物件。當我洗掉時,所有串列都會一次洗掉
這是我的看法
PopupMenuButton(
itemBuilder: (context) => [
PopupMenuItem(
onTap: () {
var list = inventoryController
.myInventoryList1
.where((i) =>
i.description !=
inventoryController
.myInventoryList1[
index]
.description)
.toList();
inventoryController
.deleteInventory(
list, context);
},
value: 1,
child: Padding(
padding:
const EdgeInsets.all(
8.0),
child: Text(
"Delete",
style: TextStyle(
color: AppColors
.pinkAppBar,
fontWeight:
FontWeight.w700),
),
),
),
uj5u.com熱心網友回復:
您可以使用removeWhere
PopupMenuItem(
onTap: () {
var list = inventoryController
.myInventoryList1;
//if you want to remove a single object from list
list.removeWhere((i) =>
i.description ==
list[index].description);
//if you want the only element in the list.
var updateList = list.firstWhere((i) => i.description ==
list[index].description)
inventoryController
.deleteInventory(
list, context);
}
uj5u.com熱心網友回復:
將物件和索引洗掉到串列中
List.remove(物件值)
將物件洗掉到串列中的示例
串列 l = [1, 2, 3,4,5,6,7,8,9];
bool res = l.remove(1);
結果
[2、3、4、5、6、7、8、9]
List.removeAt(int index)
將索引洗掉到串列中的示例
串列 l = [1, 2, 3,4,5,6,7,8,9];
bool res = l.removeAt(1);
結果
[1、3、4、5、6、7、8、9]
uj5u.com熱心網友回復:
試試這個方法這里,List 包含 Inventory 類的所有資料。通過 List 物件查找或洗掉資料。
List<<Inventory>Inventory> list;
//For removing specific item from a list with the attribute value
list.removeWhere((item) => item.id == '001')
//Remove item by specifying the position of the item in the list
list.removeAt(2)
//Remove last item from the list
list.removeLast()
//Remove a range of items from the list
list.removeRange(2,5)
如果您提出任何問題,請向我們提出您的問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/460126.html
