如何从列表视图中删除某些内容并重新加载页面?颤振网
How do I delete something from listview and reload the page? Flutter Web
您好,我正在尝试执行删除请求。我成功了,但是它没有刷新页面。用户必须手动刷新页面才能看到更改。我尝试使用 setState((){}) 但没有用。
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,
),
],
),
),
),
],
);
},
);
}
}
您还必须在小部件列表中删除:
onPressed: () {
setState(() {
widget.todoService
.deleteTodo(id: widget.todos[index].id);
widget.todos.removeWhere((item) => item.id == widget.todos[index].id);
});
您好,我正在尝试执行删除请求。我成功了,但是它没有刷新页面。用户必须手动刷新页面才能看到更改。我尝试使用 setState((){}) 但没有用。
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,
),
],
),
),
),
],
);
},
);
}
}
您还必须在小部件列表中删除:
onPressed: () {
setState(() {
widget.todoService
.deleteTodo(id: widget.todos[index].id);
widget.todos.removeWhere((item) => item.id == widget.todos[index].id);
});