If you want to update UI after removing items from List. To fix that you should move the deletion logic to the parent. And make sure that the parent correctly calls setState accordingly. This would translate into passing a callback to your list item, which will be called on deletion.
Here is a quick example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | class MyList extends StatefulWidget { @override _MyListState createState() => _MyListState(); } class _MyListState extends State<MyList> { List<String> list = List.generate(100, (i) => i.toString()); @override Widget build(BuildContext context) { return ListView.builder( itemCount: list.length, itemBuilder: (context, index) { return MyItem(list[index], onDelete: () => removeItem(index)); }, ); } void removeItem(int index) { setState(() { list = List.from(list) ..removeAt(index); }); } } class MyItem extends StatelessWidget { final String title; final VoidCallback onDelete; MyItem(this.title, {this.onDelete}); @override Widget build(BuildContext context) { return ListTile( title: Text(this.title), onTap: this.onDelete, ); } } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.