Flutter 颤振列表视图:当onDismissed时,只调用列表上的一个项目名称
我正在使用ListView.Builder将我的数据显示到屏幕上,该屏幕工作得非常好,然后我实现了Disclose和onDismiss功能,该功能似乎工作得很好,但如果我尝试关闭项目-a,它将从屏幕上关闭该项目,但在showSnackBar上显示的项目-Z已被删除,如果我删除了item-B,它仍然会在showSnackBar上显示item-Z的名称,我向数据列表中添加了更多的项目,使item-Z成为item-X,但是如果我滑动它,它仍然会显示新的item-Z已被删除,那么我尝试删除最后一个项目,即item-Z,它会给我一个错误 [.动画库捕获到异常 ═════════════════════════════════ 引发了以下RangeError 通知AnimationController的侦听器时: 范围错误(索引):无效值:不在包含范围0..3:4 当抛出异常时,这是堆栈 #0列表。[](dart:core patch/Growtable_阵列。dart:177:60) #1 _ListsState.build..包:另一个_测试/列表_小部件。dart:81。] 这是下面的代码Flutter 颤振列表视图:当onDismissed时,只调用列表上的一个项目名称,flutter,android-layout,flutter-layout,flutter-test,Flutter,Android Layout,Flutter Layout,Flutter Test,我正在使用ListView.Builder将我的数据显示到屏幕上,该屏幕工作得非常好,然后我实现了Disclose和onDismiss功能,该功能似乎工作得很好,但如果我尝试关闭项目-a,它将从屏幕上关闭该项目,但在showSnackBar上显示的项目-Z已被删除,如果我删除了item-B,它仍然会在showSnackBar上显示item-Z的名称,我向数据列表中添加了更多的项目,使item-Z成为item-X,但是如果我滑动它,它仍然会显示新的item-Z已被删除,那么我尝试删除最后一个项目,
import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';
import './list_model.dart';
class Lists extends StatefulWidget {
@override
_ListsState createState() => _ListsState();
}
class _ListsState extends State<Lists> {
List<ItemLists> items = [
ItemLists(
title: 'Best Music of the Year',
discription: 'Davido',
favorite: false,
),
ItemLists(
title: 'Best Album Cover design',
discription: 'Brighter Press',
favorite: false,
),
ItemLists(
title: 'Best Vocalist',
discription: 'Simi-Sola',
favorite: false,
),
ItemLists(
title: 'Best Danced',
discription: 'Black Camaru',
favorite: false,
),
ItemLists(
title: 'Best Performance',
discription: 'Shofeni-Were',
favorite: false,
),
ItemLists(
title: 'Best Act',
discription: 'You Want to See Craze',
favorite: false,
),
];
@override
Widget build(BuildContext context) {
return Container(
child: ListView.builder(
scrollDirection: Axis.vertical,
shrinkWrap: true,
itemBuilder: (context, index) {
return Dismissible(
key: ObjectKey(items[index]),
background: Container(
color: Colors.red,
),
child: Card(
child: ListTile(
leading: new IconButton(
icon: Icon(
Icons.star,
color: items[index].favorite ? Colors.green : Colors.grey,
),
tooltip: 'Add to Favorite',
onPressed: () {
setState(() {
items[index].favorite = !items[index].favorite;
});
}),
title: Text('${items[index].title}'),
subtitle: Text('${items[index].discription}'),
trailing:
IconButton(icon: Icon(Icons.calendar_today), onPressed: null),
)),
onDismissed: (direction) {
// Remove the item from the data source.
setState(() {
items.removeAt(index);
});
Scaffold.of(context).showSnackBar(
SnackBar(
content: Text('${items[index]?.title} Deleted'),
),
);
},
);
},
itemCount: items.length,
),
);
}
}
class ItemLists {
String title;
String discription;
bool favorite;
ItemLists({this.title, this.discription, this.favorite});
}
从下面的屏幕截图中,你会看到我删除了一个不同的项目,但它仍然显示我删除了页面上的最后一个项目
Snackbar正在尝试获取名称,但此时该项目已被删除。解决方案是在从列表中删除项目之前获取标题名 更改为:
onDismissed: (direction) {
// Remove the item from the data source.
// get the title of the item before it is deleted
final String myTitle = items[index].title;
setState(() {
items.removeAt(index);
});
Scaffold.of(context).showSnackBar(
SnackBar(
content: Text('$myTitle Deleted'),
),
);
};
将密钥更改为某种独特的内容,例如
key:ObjectKey(items[index])。title
我们可以看到之后会发生什么。修复了该问题。。。但是我有一个问题,你怎么知道问题出在哪里?或者是什么导致了这个问题,因为根据我的理解,你刚刚找出了问题的来源,然后你检查了什么是错误的。。。请问我如何理解颤振到那个水平?当我看到一些代码示例时,我能够编写代码,但实际上理解问题应该来自哪里,而不是德堡控制台显示的问题,这对我来说是非常困难的,我总是要来这里发布issyhere。。。我很高兴你问了这个问题,我也很乐意为你指出正确的方向。如果我遇到与您类似的问题,我要做的第一件事是设置一个断点来检查我的列表,并查看在从列表中删除项目时发生的情况。如果您对设置断点和检查变量一无所知,我强烈建议您花30分钟或一小时来学习。谷歌“断点vscode”(如果你使用的话,也可以是android studio)。它将为您省去很多麻烦,并帮助您确定错误发生的位置。具体到您的问题,我知道问题与您访问列表的方式有关,因为您正在使用其索引删除X。但是,当您使用相同的索引获取项目X的名称时,您将获取项目Y的名称。这意味着列表在删除和获取名称之间发生了一些事情。最后,删除列表中的最后一项后,由于列表在该点为空,因此在尝试获取其名称时会出现RangeError。如果设置断点(例如在final String myTitle=items[index]行处.title;
,应用程序将在每次运行该行时暂停,您可以检查项目列表的内容。如果一切正常,请单击“恢复”按钮。当您对代码满意时,删除断点。这样您就可以知道您的代码是否按预期的方式运行。非常感谢您…我非常感激关于解释,最后一件事是有没有任何视频教程可以让你深入了解颤振