-1

Hello I am trying to implement a delete request. I successfully did it, however it is not refreshing the page. The use has to manually refresh the page in order to see the changes. I tried using setState((){}) but that did not work.

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,
                    ),
                  ],
                ),
              ),
            ),
          ],
        );
      },
    );
  }
}
Jai
  • 15
  • 4

1 Answers1

0

You have also to delete in list of the widget:

    onPressed: () {
                            setState(() {
                              widget.todoService
                                  .deleteTodo(id: widget.todos[index].id);
                              widget.todos.removeWhere((item) => item.id == widget.todos[index].id);


                            });
Moo
  • 670
  • 1
  • 6
  • 11