'how to delete an object from list in flutter

i want to delete an object from list of inventory in which i just have description and url of the inventory and i want to delete object of inventory by description so how can i delete the object.

function in service class

this is my service class

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');
    }
  }

This is my Controller class

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);
    });
  }

please tell me what logic i have to write in view to delete the object. when i am deleting then all list is deleting at a time

This is my view

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),
                                                ),
                                              ),
                                            ),
                                          


Solution 1:[1]

You can use 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);

        }
 

Solution 2:[2]


Removing Objects and indexes into the list


List.remove(Object value)

Example of Removing Objects into the list

List l = [1, 2, 3,4,5,6,7,8,9];

bool res = l.remove(1);

Result

[2, 3, 4, 5, 6, 7, 8, 9]

List.removeAt(int index)

Example of Removing Index into the list

List l = [1, 2, 3,4,5,6,7,8,9];

bool res = l.removeAt(1);

result

[1, 3, 4, 5, 6, 7, 8, 9]

Solution 3:[3]

Try this way Here, List contains Inventory class all data. Find or remove data by List object.

List<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)

If you raise any issue raise your query to us.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Mustafa Raza
Solution 3