Consider a Flutter app where a ListView shows items from a list. When the list changes, the ListView updates automatically. Why does this happen?
class MyList extends StatefulWidget { @override State<MyList> createState() => _MyListState(); } class _MyListState extends State<MyList> { List<String> items = ['Apple', 'Banana']; void addItem() { setState(() { items.add('Orange'); }); } @override Widget build(BuildContext context) { return ListView.builder( itemCount: items.length, itemBuilder: (context, index) { return Text(items[index]); }, ); } }
Think about what triggers Flutter to redraw widgets.
Flutter rebuilds widgets when setState() is called. This causes the ListView to read the updated list and display the new data.
In Flutter's ListView.builder, what does the itemCount property control?
ListView.builder(
itemCount: items.length,
itemBuilder: (context, index) {
return Text(items[index]);
},
);Think about how the list knows how many items to show.
The itemCount tells ListView.builder how many times to call itemBuilder, matching the current list length.
In a StatefulWidget, if you update the list data but do not call setState(), what happens to the UI?
class MyWidget extends StatefulWidget { @override State<MyWidget> createState() => _MyWidgetState(); } class _MyWidgetState extends State<MyWidget> { List<String> items = ['A', 'B']; void updateList() { items.add('C'); // Missing setState() } @override Widget build(BuildContext context) { return ListView.builder( itemCount: items.length, itemBuilder: (context, index) => Text(items[index]), ); } }
Remember how Flutter knows when to redraw widgets.
Without calling setState(), Flutter does not rebuild the widget, so UI stays the same even if data changed.
Given this code, why does the ListView not show the new item?
class MyList extends StatefulWidget { @override State<MyList> createState() => _MyListState(); } class _MyListState extends State<MyList> { List<String> items = ['One', 'Two']; void addItem() { items = [...items, 'Three']; // Missing setState() } @override Widget build(BuildContext context) { return ListView.builder( itemCount: items.length, itemBuilder: (context, index) => Text(items[index]), ); } }
Think about what triggers UI updates in Flutter.
Even if the list variable is reassigned, without setState() Flutter does not rebuild the widget tree to show changes.
In a Flutter app, you have a ListView showing dynamic data. When the list updates, the scroll position resets to the top. How can you keep the scroll position?
class MyList extends StatefulWidget { @override State<MyList> createState() => _MyListState(); } class _MyListState extends State<MyList> { final ScrollController _controller = ScrollController(); List<String> items = ['Item 1', 'Item 2']; void addItem() { setState(() { items.add('Item 3'); }); } @override Widget build(BuildContext context) { return ListView.builder( controller: _controller, itemCount: items.length, itemBuilder: (context, index) => Text(items[index]), ); } }
Think about how Flutter manages scroll positions with controllers.
Attaching a ScrollController to ListView preserves scroll position across rebuilds automatically.