Consider this Flutter code snippet that builds a list of 3 items:
ListView.builder(
itemCount: 3,
itemBuilder: (context, index) {
return Text('Item $index');
},
)What will the user see on the screen?
ListView.builder( itemCount: 3, itemBuilder: (context, index) { return Text('Item $index'); }, )
Remember that index starts at 0 and goes up to itemCount - 1.
The ListView.builder creates widgets for indices 0, 1, and 2 because itemCount is 3. So it shows 'Item 0', 'Item 1', and 'Item 2'.
In Flutter, when does the itemBuilder function of ListView.builder get called?
Think about how Flutter optimizes scrolling lists.
ListView.builder calls itemBuilder lazily, only when an item scrolls into view, to save memory and improve performance.
Identify which ListView.builder code snippet will cause a syntax error.
ListView.builder( itemCount: 5, itemBuilder: (context, index) { return Text('Item $index'); } )
Check the syntax of function parameters and arrow functions in Dart.
Option D misses a comma between parameters, causing a syntax error. Option D uses a block with arrow syntax incorrectly.
You want to navigate to a detail screen when a user taps an item in ListView.builder. Which code snippet correctly adds tap handling?
ListView.builder( itemCount: 3, itemBuilder: (context, index) { return Text('Item $index'); }, )
Remember which widgets handle taps in Flutter.
Text widget does not have onTap or onPressed. GestureDetector or InkWell can detect taps and trigger navigation.
Given this code snippet inside a Column:
Column(
children: [
Text('Header'),
ListView.builder(
itemCount: 3,
itemBuilder: (context, index) => Text('Item $index'),
),
],
)Why does this cause a 'RenderBox was not laid out' error?
Think about how ListView behaves inside unbounded height parents.
ListView.builder tries to expand infinitely inside a Column because Column does not constrain height, causing layout errors.