flutter - 发回数据后列表视图不会更新(颤振)
问题描述
由于我是 Flutter 的新手,我为多个问题道歉。
我正在尝试从颤动的路线发回数据(列表项索引)以从 ListView 中删除。但是,ListView 没有更新。不知道我在这里做错了什么。我已经缩短了一些代码,但是如果您需要更多代码,请告诉我。我感谢您的帮助。
List clients;
Appointments({Key key, @required this.clients}): super(key: key);
@override
State<StatefulWidget> createState() {
return AppointmentState();
}
}
class AppointmentState extends State<Appointments> {
@override
Widget build (BuildContext context) {
return Container(
child: Expanded(
child: ListView.builder(
itemCount: widget.clients.length,
itemBuilder: (context, index) {
var client = widget.clients[index];
return Dismissible(
key: Key(client),
direction:DismissDirection.endToStart,
onDismissed: (direction) {
setState () {
widget.clients.removeAt(index);
}
},
background: Container(color: Colors.red),
child: ListTile(
title: Text(client),
leading: Icon(Icons.face),
trailing: Icon(Icons.keyboard_arrow_right),
onTap: () {
//list index, and index data (client)
_checkIfRemoved(context, index, client);
}
),
);...
_checkIfRemoved 方法:
void _checkIfRemoved (BuildContext context, index, client) async {
final result = await Navigator.push(context, MaterialPageRoute(builder: (context) => Client(clientIndex: index, clientName: client)));
setState() {
widget.clients.removeAt(result);
}
print(result);
}
客户端画面
RaisedButton(
child: Text("Remove Client"),
color: Colors.red,
onPressed: () {
Navigator.pop(context, widget.clientIndex);
},
)
解决方案
setState
您使用的不正确
改变这个:
setState() {
widget.clients.removeAt(result);
}
对此:
setState(() {
widget.clients.removeAt(result);
});
推荐阅读
- asp.net-core - 如何在 Azure Bot 中调用需要身份验证的 API?
- angular - 如何手动完成 angular http observable
- angular - 开始日期验证之前的结束日期在 Angular 项目中无法正常工作
- c# - 如何在 Unity3D 中使按钮的可选区域更小?
- sql - 如何查询一张表,并将结果作为两张表返回
- javascript - 如何阻止发布方法表单清除提交时的所有输入值?(HTML/JS)
- python - gc.collect() 函数究竟做了什么?
- google-cloud-pubsub - 在 pubsub 服务中处理 SIGTERM 和 SIGINT 信号的最佳方法是什么?
- python-3.x - Python Selenium 循环遍历表元素
- machine-learning - 在 Triplet loss 中增加 alpha 有什么缺点吗?