I am trying to do a todoapp with Provider but it is not working as expected. In my HomeScreen, I have a list of tasks, which is being stored in the provider. To edit a task, I go to another screen, TaskScreen, where I submit a form and, ideally, it should update the list, but it does not. Actually it does, but only after a hot reload, it is not synchronized.
class Task extends StatelessWidget {
String title;
Task({super.key, required this.title});
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () => {
Navigator.of(context).pushNamed(
'/task',
arguments: TitleTask(title: title),
),
FocusManager.instance.primaryFocus?.unfocus(),
},
child: Dismissible(
key: Key(title),
child: Container(
decoration: BoxDecoration(
border: Border.all(
color: Colors.blue,
width: 1,
style: BorderStyle.solid,
),
borderRadius: BorderRadius.circular(10),
color: Colors.grey[300],
),
margin: const EdgeInsets.only(bottom: 8.0),
child: ListTile(
title: Text(
title,
style: const TextStyle(fontSize: 18),
),
trailing: Wrap(
spacing: 0,
children: <IconButton>[
IconButton(
padding: EdgeInsets.zero,
icon: const Icon(Icons.delete),
color: Colors.red,
onPressed: () => context.read<Tasks>().delete(title),
),
IconButton(
padding: EdgeInsets.zero,
icon: const Icon(Icons.check),
color: Colors.green,
onPressed: () => {},
)
],
),
),
),
onDismissed: (direction) => context.read<Tasks>().delete(title),
),
);
}
}
class Tasks with ChangeNotifier {
final List<Task> _tasks = [];
List get tasks => _tasks;
void add(String title) {
_tasks.add(Task(title: title));
notifyListeners();
}
void delete(String title) {
_tasks.removeWhere((element) => element.title == title);
notifyListeners();
}
void edit(String? taskTitle, String newTaskTitle) {
_tasks[_tasks.indexWhere((element) => element.title == taskTitle)].title =
newTaskTitle;
notifyListeners();
}
}
I am building the list of tasks like this:
ListView.builder(
scrollDirection: Axis.vertical,
shrinkWrap: true,
itemCount: context.watch<Tasks>().tasks.length,
itemBuilder: (context, index) {
final item = context.watch<Tasks>().tasks[index];
return item;
},
Does anyone know what is happening? I fear it may be related to the provider not knowing that it needs to update, because it knows the value and it doesn't update.