Flutter 如何从listview中删除某些内容并重新加载页面?颤振腹板
您好,我正在尝试实现一个删除请求。我成功地做到了,但是它没有刷新页面。用户必须手动刷新页面才能看到更改。我尝试使用setState((){}),但没有成功Flutter 如何从listview中删除某些内容并重新加载页面?颤振腹板,flutter,flutter-web,http-delete,Flutter,Flutter Web,Http Delete,您好,我正在尝试实现一个删除请求。我成功地做到了,但是它没有刷新页面。用户必须手动刷新页面才能看到更改。我尝试使用setState((){}),但没有成功 class TodoList extends StatefulWidget { final List<dynamic> todos; final TodoService todoService; TodoList({Key key, this.todos, this.todoService}) : super(key:
class TodoList extends StatefulWidget {
final List<dynamic> todos;
final TodoService todoService;
TodoList({Key key, this.todos, this.todoService}) : super(key: key);
@override
_TodoListState createState() => _TodoListState();
}
class _TodoListState extends State<TodoList> {
@override
Widget build(BuildContext context) {
return ListView.builder(
itemCount: widget.todos.length,
itemBuilder: (context, index) {
return Column(
children: <Widget>[
Card(
child: ListTile(
title: Text(widget.todos[index].description),
trailing: Wrap(
children: <Widget>[
IconButton(
icon: Icon(Icons.edit),
onPressed: () {},
color: Colors.blue,
),
SizedBox(width: 20),
IconButton(
icon: Icon(Icons.delete),
onPressed: () {
setState(() {
widget.todoService
.deleteTodo(id: widget.todos[index].id);
});
},
color: Colors.red,
),
],
),
),
),
],
);
},
);
}
}
类TodoList扩展了StatefulWidget{
最后的待办事项清单;
最终TodoService TodoService;
TodoList({Key-Key,this.todos,this.todoService}):super(Key:Key);
@凌驾
_TodoListState createState()=>\u TodoListState();
}
类_TodoListState扩展了状态{
@凌驾
小部件构建(构建上下文){
返回ListView.builder(
itemCount:widget.todos.length,
itemBuilder:(上下文,索引){
返回列(
儿童:[
卡片(
孩子:ListTile(
标题:文本(widget.todos[index].description),
尾随:包裹(
儿童:[
图标按钮(
图标:图标(Icons.edit),
按下:(){},
颜色:颜色,蓝色,
),
尺寸箱(宽度:20),
图标按钮(
图标:图标(Icons.delete),
已按下:(){
设置状态(){
widget.todoService
.deleteTodo(id:widget.todos[index].id);
});
},
颜色:颜色,红色,
),
],
),
),
),
],
);
},
);
}
}
您还必须在小部件列表中删除:
onPressed: () {
setState(() {
widget.todoService
.deleteTodo(id: widget.todos[index].id);
widget.todos.removeWhere((item) => item.id == widget.todos[index].id);
});