在Flutter中实现可弃用小部件时出错

时间:2020-10-17 12:27:42

标签: flutter dart flutter-listview

我已经在flutter应用程序中实现了Dismissible小部件,以滑动并删除索引的项目。当我从列表中删除任何项目时,我在日志中得到以下错误

错误

RangeError(索引):无效值:不在包含范围0..48:49

这是已实现的代码

CODE

        SingleChildScrollView(

          child: Column(
            mainAxisAlignment: MainAxisAlignment.end,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
            
              ListView.separated(
                separatorBuilder: (context, index){
                  return Divider();
                },
                controller: _scrollController,
                itemCount: noteItems,
                shrinkWrap: true,
                itemBuilder: (context, index) {
                  final item = firstdata[index];
                  String arr = firstdata[index]['created_at'];
                  var arr2 = arr.split('-');
                  var content = firstdata[index]['company_details']['logo_name'];
                  print('logo name check');
                  print(content);

                  return
                    Dismissible(
                    direction: DismissDirection.endToStart,
                    key: ObjectKey(item),
                    onDismissed: (direction) {

                      deleteSingleNotifications(userid.toString(),firstdata[index]['id']);
                      setState(() {
                        firstdata.removeAt(index);
                      });


                    },
                    background: Container(color: Colors.red, child: Center(child: Text("DELETE", style: TextStyle(fontSize:50, fontWeight: FontWeight.bold),)),)
                    ,

                    child: Padding(

                      padding: const EdgeInsets.fromLTRB(8.0, 7.0, 8.0, 0.0),
                      child: Column(
                        children: <Widget>[


                          firstdata[index]['status'] == "0"?
                      Container(
                      color:Colors.grey.shade200,
                        child: ListTile(
                         

                          title:
                          Row(
                            children: [


                              Flexible(
                                child: firstdata[index]['title']!= null?AutoSizeText(
                                  firstdata[index]['title'],
                                  maxLines: 2,


                                  style: TextStyle(fontWeight: FontWeight.bold),) :Text(''),
                              ),
                            ],),



                        ),
                      )
                              :Container(
                            color: Colors.white,
                            child: ListTile(
                             
                             

                              title:
                              Row(
                                children: [


                                  Flexible(
                                    child: firstdata[index]['title']!= null?AutoSizeText(
                                      firstdata[index]['title'],
                                      maxLines: 2,


                                      style: TextStyle(fontWeight: FontWeight.bold),) :Text(''),
                                  ),

                                ],),



                            ),
                          ),
              ],
                      ),
                    ),
                  );

                },

              ),
            ],
          ),
        )

当我删除列表项后点击任何项时,都会出现以下错误。

错误

方法'+'在null上被调用。接收者:null

我应该如何解决此类问题?

1 个答案:

答案 0 :(得分:0)

我认为这是与您的Listview长度有关的问题。似乎您将其设置为49而不是48。请尝试以下操作:

    itemCount: noteItems - 1,