flutter 我想在单击按钮时按索引删除列表中添加的项目

jxct1oxe  于 2023-01-27  发布在  Flutter
关注(0)|答案(2)|浏览(122)

我有一个listview。builder与数据,你可以点击,他们将被添加到列表中,在此列表后,我想显示在另一个屏幕上(我通过提供程序)。当点击所选数据很长一段时间,他们应该从列表中删除,但我一直得到一个错误RangeError(索引):无效值:不在包含范围0..2内:5
我的主屏幕

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
6yjfywim

6yjfywim1#

发生这种情况是因为你的listview构建在searchResult列表上,但是你尝试在marksList上使用它的索引。你有两个选择,要么用marksList创建列表,要么改变你的删除函数并在searchResult上这样做。

tzxcd3kk

tzxcd3kk2#

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

您还可以调整开始和结束日期以及起始行号,以满足您的需要。
我想这会对你有帮助。

相关问题