Home > Software design >  I want to delete the added item in the list by index when clicking on the button
I want to delete the added item in the list by index when clicking on the button

Time:01-26

I have a listview.builder with data that you can click on and they will be added to the List, after this List I want to display on another screen (I do it through the provider). When clicking on the selected data for a long time, they should be deleted from the List, but I keep getting an error RangeError(index): Invalid value: Not in inclusive range 0..2: 5

my main screen

ListView.builder(
                        shrinkWrap: true,
                          physics: NeverScrollableScrollPhysics(),
                          itemCount: searchMethodProvider.searchResult.length,
                          itemBuilder: (context, index) {
                            return Material(
                              color: Colors.transparent,
                              child: InkWell(
                                onLongPress: (){
                                  setState(() {
                                    searchMethodProvider.searchResult[index]['bool'] = true;

                                    var modelApp = searchMethodProvider.marksList[index]['model'];
                                    var indexApp = searchMethodProvider.marksList[index]['index'];

                                    searchMethodProvider.addMark(indexApp, modelApp);
                                  });
                                },
                                onTap: (){
                                  setState(() {
                                    searchMethodProvider.deleteDataIndex(index);
                                    print(searchMethodProvider.dataMark);// searchMethodProvider.deleteDataIndex(index, context);
                                    searchMethodProvider.searchResult[index]['bool'] = false;
                                  });
                                },
                                child: Container(
                                  height: 53,
                                  width: double.infinity,
                                  decoration: BoxDecoration(
                                    border: Border(
                                      top: BorderSide(
                                        width: 0.8,
                                        color: Color.fromRGBO(237, 237, 237, 1)
                                      ),
                                    ),
                                  ),
                                  child: Row(
                                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                    children: [
                                      Align(
                                        alignment: Alignment.centerLeft,
                                        child: Padding(
                                          padding: const EdgeInsets.only(
                                            left: 14.22
                                          ),
                                          child: Text(
                                            searchMethodProvider.searchResult[index]['mark'],
                                            style: TextStyle(
                                              fontFamily: ConstantsFonts.sfProRegular,
                                              fontSize: 16,
                                              color: Color.fromRGBO(0, 0, 0, 1)
                                            ),
                                          ),
                                        ),
                                      ),
                                      searchMethodProvider.searchResult[index]['bool'] == true ?
                                      Icon(
                                        Icons.keyboard_arrow_down,
                                        size: 18,
                                        color:  Color.fromRGBO(87, 184, 238, 1),
                                      ) : Container()
                                    ],
                                  ),
                                ),
                              ),
                            );
                          }
                      )
this is my provider and him functions

    class SearchMethodInMarkFilterProvider extends ChangeNotifier {

      List<Map<String, dynamic>> marksList = [
        {
          'mark': 'Aston Martin',
          'bool': false,
          'index': 1,
          'model': ['x5', '23']
        },
        {
          'mark': 'Audi',
          'bool': false,
          'index': 2,
          'model': ['x5', '23']
        },
        {
          'mark': 'BMW',
          'bool': false,
          'index': 3,
          'model': ['x5', '23']
        },
      ];

      List dataMark = [];

      Map addData = {};

      List<Map<String, dynamic>> searchResult = [];

      void addMark(int indexApp, List markData){
        addData = {
          'indexApp': indexApp,
          'markData': markData,
        };
          dataMark.add(addData);
          print(dataMark);
      }

      void deleteDataIndex(int index){
        dataMark.removeAt(index);
        notifyListeners();
      }

      SearchMethodInMarkFilterProvider(){
        searchResult = marksList;
      }

      void runFilter(String enteredKeyword) {
        List<Map<String, dynamic>> results = [];
        if (enteredKeyword.isEmpty) {
          results = marksList;
        } else {
          results = marksList
              .where((user) =>
              user['mark'].toLowerCase().contains(enteredKeyword.toLowerCase()))
              .toList();
        }

        searchResult = results;
        notifyListeners();
      }

    }

maybe I'm creating the data model incorrectly, that's why I can't delete it properly, I'll be glad of any help

CodePudding user response:

This happened because your listview build on the searchResult list, but you try use its index on marksList. You have two options, either make list with marksList or change your delete function and do that on searchResult.

CodePudding user response:

Sub AddStartOfMonthDates()
    'set the starting date
    Dim startDate As Date
    startDate = DateValue("01/01/2021")

    'set the ending date
    Dim endDate As Date
    endDate = DateValue("12/31/2021")

    'set the starting row for the dates
    Dim rowNum As Integer
    rowNum = 1

    'loop through the dates
    Do While startDate <= endDate
        'add the date to the column
        Cells(rowNum, 1).Value = startDate

        'increment the row number
        rowNum = rowNum   1

        'move to the next month
        startDate = DateAdd("m", 1, startDate)
    Loop
End Sub

You can also adjust the starting and ending dates, as well as the starting row number, to suit your needs.

I think it will help you.

  • Related