点击Contacts/Leads/Junk按钮出现Invalid range错误,昨日可正常访问数据
解决Flutter中点击按钮触发Invalid range错误的问题
昨天点击Contacts、Leads、Junk按钮均可正常访问对应数据,今日执行相同操作时触发Invalid range错误。以下是Leads按钮对应的Flutter代码片段:
Expanded( child: GestureDetector( child: Container( width: MediaQuery(context) .size .width * 0.30, // margin: // EdgeInsets.only(top: 10), child: Wrap( children: [ ClipRRect( borderRadius: BorderRadius .circular( 36.0), child: Container( padding: const EdgeInsets .only( top: 10.0, bottom: 10.0, left: 6.0, right: 6.0), decoration: const BoxDecoration( color: Color( 0xFFD7EFD2), boxShadow: [ BoxShadow( color: Color( 0xFFD7EFD2), offset: Offset( 3.0, 6.0), blurRadius: 10.0), ], ), child: Padding( padding: const EdgeInsets .symmetric( horizontal: 5.0), child: Row( mainAxisAlignment: MainAxisAlignment .start, crossAxisAlignment: CrossAxisAlignment .center, children: [ Container( padding: EdgeInsets.zero, margin: EdgeInsets.zero, // alignment: // Alignment // .centerLeft, width: MediaQuery(context).size.width * .18, child: Text( 'Leads' .toUpperCase(), style: const TextStyle( fontSize: 10.0, fontFamily: "OpenSans", color: Colors.black, fontWeight: FontWeight.bold)), ), Expanded( child: Text( mapDataLead[collectionList[index]["Name"]] .toString(), style: const TextStyle( fontSize: 10.0, color: Colors.black, fontWeight: FontWeight.bold, fontFamily: "OpenSans"), )), ], ), )), ), ], ), ), onTap: () { Navigator.push( context, MaterialPageRoute( builder: (context) => ContactListTabScreen( "Collection:" + collectionList[index] [ "Name"] + "|", "Leads"))); }, ), )
可能的原因及排查方向
Invalid range错误一般是由于访问了超出有效范围的索引(数组/列表)或字符串截取范围无效导致的,结合代码可从以下几点排查:
- 索引越界问题:检查
collectionList[index]中的index是否超出collectionList的有效索引范围。今日数据更新后,collectionList的长度可能发生变化,导致原本合法的index现在越界。 - 键不存在问题:确认
collectionList[index]["Name"]对应的键是否存在于mapDataLead中。如果键不存在,mapDataLead[...]会返回null,后续调用toString()可能间接引发连锁错误,或者传递到ContactListTabScreen后处理出错。 - 目标页面逻辑问题:排查
ContactListTabScreen接收参数后的处理逻辑,比如解析"Collection:" + collectionList[index]["Name"] + "|"这个字符串时,是否有截取、分割等操作导致范围错误,或是页面内访问列表索引时越界。
具体排查步骤
- 打印
collectionList.length和index的值,验证index是否在0到collectionList.length - 1之间。 - 打印
collectionList[index]["Name"]和mapDataLead.keys,确认键存在性。 - 在
ContactListTabScreen的初始化或参数处理代码中添加日志,追踪参数解析和数据访问的过程,定位具体的错误触发点。
内容的提问来源于stack exchange,提问作者manoj kumar
相关产品推荐
相关产品推荐

