I have a ListView that generates ListTiles after reading data from an SQFlite database. Each tile has an IconButton that allows user to delete that row. The delete operation works fine, but the ListView does not refresh and the items are still visible. How can I get the application to refresh data after the delete operation?
class TaskListState extends State<TaskList> {
DBProvider dbProvider = new DBProvider();
Future<List<Task>> tasks;
@override
void initState() {
tasks = dbProvider.getAllTasks();
super.initState();
}
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: tasks,
builder: (context, snapshot){
if(snapshot.hasError){
return Text("Data has error.");
} else if (!snapshot.hasData){
return Text("Wait please...");
} else {
return taskList(snapshot.data);
}
},
);
}
Widget taskList(List<Task> tasks){
return ListView.builder(
itemCount: tasks.length,
itemBuilder: (context, index){
String _subtitle = tasks[index].date + " " + tasks[index].start + "-" + tasks[index].end;
return Card(
elevation: 2.0,
child: ListTile(
leading: Icon(Icons.face),
title: Text(tasks[index].name),
subtitle: Text(_subtitle),
onTap: (){print("Tapped");},
trailing: IconButton(
icon: Icon(Icons.close),
onPressed: (){
dbProvider.delete(tasks[index].id);
},
),
),
);
},
);
}
}
> cannot be assigned to List
– mominbay Nov 03 '19 at 07:45