【问题标题】:I want to delete the added item in the list by index when clicking on the button单击按钮时,我想按索引删除列表中添加的项目
【发布时间】:2023-01-25 21:11:05
【问题描述】:

我有一个 listview.builder,其中包含您可以单击的数据,它们将被添加到列表中,在此列表之后,我想在另一个屏幕上显示(我通过提供商来完成)。当长时间点击选中的数据时,它们应该从列表中删除,但我一直收到错误 RangeError(index): Invalid value: Not in inclusive range 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

【问题讨论】:

  • 您能否添加完整的代码以便我们重现该问题。
  • 请分享完整代码
  • 更新了问题
  • @eamirho3ein 添加。
  • @WaliKhan 添加

标签: flutter dart flutter-listview


【解决方案1】:
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

您还可以调整开始日期和结束日期以及开始行号,以满足您的需要。

我认为它会对你有所帮助。

【讨论】:

    【解决方案2】:

    发生这种情况是因为您的 listview 构建在 searchResult 列表上,但您尝试在 marksList 上使用它的索引。您有两个选择,要么使用marksList 制作列表,要么更改您的删除功能并在searchResult 上执行此操作。

    【讨论】:

    • 谢谢你的回答,我明白你的意思
    • @Skeleteenx 很乐意提供帮助。
    猜你喜欢
    • 2021-10-20
    • 2016-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-02
    • 1970-01-01
    • 1970-01-01
    • 2020-09-15
    相关资源
    最近更新 更多