Flutter AnimatedList在删除最后一项时引发错误
我正在使用一个Flutter AnimatedList在删除最后一项时引发错误,flutter,flutter-animation,Flutter,Flutter Animation,我正在使用一个AnimatedList来可视化删除和插入列表中的项目。当我删除列表底部的项目时,会引发以下错误:RangeError(索引):无效值:有效值范围为空:0,这是当我删除列表底部的项目但仍有项目在该项目上方或RangeError(索引)时引发的错误:无效值:删除最后一项时,唯一有效值为0:1。当我删除列表顶部的项目时,代码起作用,但在删除最后一个项目时失败。是否有一个简单的解决方案,这样我可以删除我的所有项目没有问题 以下是我的代码的简化版本: class AnimatedListD
AnimatedList
来可视化删除和插入列表中的项目。当我删除列表底部的项目时,会引发以下错误:RangeError(索引):无效值:有效值范围为空:0
,这是当我删除列表底部的项目但仍有项目在该项目上方或RangeError(索引)时引发的错误:无效值:删除最后一项时,唯一有效值为0:1
。当我删除列表顶部的项目时,代码起作用,但在删除最后一个项目时失败。是否有一个简单的解决方案,这样我可以删除我的所有项目没有问题
以下是我的代码的简化版本:
class AnimatedListDemo extends StatefulWidget {
@override
_AnimatedListDemoState createState() => _AnimatedListDemoState();
}
class _AnimatedListDemoState extends State<AnimatedListDemo> {
GlobalKey<AnimatedListState> _globalKey = new GlobalKey();
List<int> items = [];
Widget _listItem(BuildContext context, int index, animation) {
return SlideTransition(
position: Tween<Offset>(
begin: const Offset(-1, 0),
end: const Offset(0, 0),
).animate(animation),
child: ListTile(
title: Text("${items[index]}"),
trailing: IconButton(
icon: Icon(Icons.delete),
onPressed: () {
setState(() {
items.removeAt(index);
_globalKey.currentState.removeItem(index,
(context, animation) => _listItem(context, index, animation));
});
},
),
),
);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("AnimatedListDemo"),
actions: [
IconButton(
icon: Icon(Icons.add),
onPressed: () {
items.add(items.length);
_globalKey.currentState.insertItem(items.length - 1);
},
)
],
),
body: AnimatedList(
key: _globalKey,
initialItemCount: items.length,
itemBuilder: (context, index, animation) =>
_listItem(context, index, animation),
),
),
);
}
}
class AnimatedListDemo扩展StatefulWidget{
@凌驾
_AnimatedListDemoState createState()=>_AnimatedListDemoState();
}
类_AnimatedListDemoState扩展状态{
GlobalKey _GlobalKey=新的GlobalKey();
列表项=[];
Widget_listItem(BuildContext上下文、int索引、动画){
返回幻灯片转换(
位置:吐温(
开始:常量偏移量(-1,0),
结束:常量偏移量(0,0),
).制作动画(动画),
孩子:ListTile(
标题:文本(“${items[index]}”),
尾随:图标按钮(
图标:图标(Icons.delete),
已按下:(){
设置状态(){
项目移除(索引);
_globalKey.currentState.removeItem(索引,
(上下文,动画)=>\u列表项(上下文,索引,动画));
});
},
),
),
);
}
@凌驾
小部件构建(构建上下文){
返回材料PP(
家:脚手架(
appBar:appBar(
标题:文本(“动画列表演示”),
行动:[
图标按钮(
图标:图标(Icons.add),
已按下:(){
项目。添加(项目。长度);
_globalKey.currentState.insertItem(items.length-1);
},
)
],
),
主体:动画列表(
键:_globalKey,
initialItemCount:items.length,
itemBuilder:(上下文、索引、动画)=>
_listItem(上下文、索引、动画),
),
),
);
}
}
问题在于,当列表互动程序中的项目[索引]
标题小部件已被删除时,您试图在该小部件中显示该项目
解决办法可以是:
title: Text(index >= items.length ? '' : items[index].toString()),
因此,完整的例子可以是:
class AnimatedListDemo extends StatefulWidget {
@override
_AnimatedListDemoState createState() => _AnimatedListDemoState();
}
class _AnimatedListDemoState extends State<AnimatedListDemo> {
GlobalKey<AnimatedListState> _globalKey = new GlobalKey();
List<int> items = [];
Widget _listItem(BuildContext context, int index, animation) {
return SlideTransition(
position: Tween<Offset>(
begin: const Offset(-1, 0),
end: const Offset(0, 0),
).animate(animation),
child: ListTile(
title: Text(index >= items.length ? '' : items[index].toString()),
trailing: IconButton(
icon: Icon(Icons.delete),
onPressed: () {
setState(() {
items.removeAt(index);
_globalKey.currentState.removeItem(index,
(context, animation) => _listItem(context, index, animation));
});
},
),
),
);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("AnimatedListDemo"),
actions: [
IconButton(
icon: Icon(Icons.add),
onPressed: () {
items.add(items.length);
_globalKey.currentState.insertItem(items.length - 1);
},
)
],
),
body: AnimatedList(
key: _globalKey,
initialItemCount: items.length,
itemBuilder: (context, index, animation) =>
_listItem(context, index, animation),
),
),
);
}
}
class AnimatedListDemo扩展StatefulWidget{
@凌驾
_AnimatedListDemoState createState()=>_AnimatedListDemoState();
}
类_AnimatedListDemoState扩展状态{
GlobalKey _GlobalKey=新的GlobalKey();
列表项=[];
Widget_listItem(BuildContext上下文、int索引、动画){
返回幻灯片转换(
位置:吐温(
开始:常量偏移量(-1,0),
结束:常量偏移量(0,0),
).制作动画(动画),
孩子:ListTile(
标题:文本(索引>=items.length?“”:items[index].toString()),
尾随:图标按钮(
图标:图标(Icons.delete),
已按下:(){
设置状态(){
项目移除(索引);
_globalKey.currentState.removeItem(索引,
(上下文,动画)=>\u列表项(上下文,索引,动画));
});
},
),
),
);
}
@凌驾
小部件构建(构建上下文){
返回材料PP(
家:脚手架(
appBar:appBar(
标题:文本(“动画列表演示”),
行动:[
图标按钮(
图标:图标(Icons.add),
已按下:(){
项目。添加(项目。长度);
_globalKey.currentState.insertItem(items.length-1);
},
)
],
),
主体:动画列表(
键:_globalKey,
initialItemCount:items.length,
itemBuilder:(上下文、索引、动画)=>
_listItem(上下文、索引、动画),
),
),
);
}
}
成功了!非常感谢。