0
0
Fluttermobile~20 mins

ListView basics in Flutter - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
ListView Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What does this ListView display?
Consider this Flutter code snippet. What will the ListView show on the screen?
Flutter
ListView(
  children: [
    Text('Apple'),
    Text('Banana'),
    Text('Cherry'),
  ],
)
AA blank screen because Text widgets need to be wrapped in Containers
BA horizontal list showing the words: Apple, Banana, Cherry
CAn error because ListView needs a builder, not children
DA vertical list showing the words: Apple, Banana, Cherry
Attempts:
2 left
💡 Hint
ListView by default scrolls vertically and accepts children widgets.
📝 Syntax
intermediate
2:00remaining
Which option correctly creates a ListView with 3 items using builder?
Choose the correct Flutter code to create a ListView with 3 Text items: 'One', 'Two', 'Three' using ListView.builder.
A
ListView.builder(
  itemCount: 3,
  itemBuilder: (context, index) {
    return Text(['One', 'Two', 'Three'][index]);
  },
)
B
ListView.builder(
  itemCount: 3,
  itemBuilder: (index, context) {
    return Text(['One', 'Two', 'Three'][index]);
  },
)
C
ListView.builder(
  itemCount: 3,
  builder: (context, index) {
    return Text(['One', 'Two', 'Three'][index]);
  },
)
D
ListView.builder(
  count: 3,
  itemBuilder: (context, index) {
    return Text(['One', 'Two', 'Three'][index]);
  },
)
Attempts:
2 left
💡 Hint
The itemBuilder function parameters are (BuildContext, int). The property is itemCount, not count.
lifecycle
advanced
2:00remaining
What happens if you update the list data but forget to call setState() in a StatefulWidget?
In a StatefulWidget, you update the list used by ListView but do not call setState(). What is the visible effect?
AThe ListView does not update and still shows old data.
BThe ListView updates immediately to show the new data.
CThe app crashes with a runtime error.
DThe ListView shows a blank screen.
Attempts:
2 left
💡 Hint
setState() tells Flutter to redraw the widget with new data.
navigation
advanced
2:00remaining
How to navigate to a new screen when tapping a ListView item?
You want to open a new screen when a user taps an item in ListView. Which code snippet correctly does this?
A
ListView.builder(
  itemCount: items.length,
  itemBuilder: (context, index) {
    return ListTile(
      title: Text(items[index]),
      onTap: () {
        Navigator.pop(context);
      },
    );
  },
)
B
ListView.builder(
  itemCount: items.length,
  itemBuilder: (context, index) {
    return ListTile(
      title: Text(items[index]),
      onPressed: () {
        Navigator.push(context, MaterialPageRoute(builder: (context) => DetailScreen()));
      },
    );
  },
)
C
ListView.builder(
  itemCount: items.length,
  itemBuilder: (context, index) {
    return ListTile(
      title: Text(items[index]),
      onTap: () {
        Navigator.push(context, MaterialPageRoute(builder: (context) => DetailScreen()));
      },
    );
  },
)
D
ListView.builder(
  itemCount: items.length,
  itemBuilder: (context, index) {
    return ListTile(
      title: Text(items[index]),
      onTap: () => Navigator.pop(context),
    );
  },
)
Attempts:
2 left
💡 Hint
Use onTap for ListTile tap events and Navigator.push to go to a new screen.
🔧 Debug
expert
2:00remaining
Why does this ListView cause a vertical overflow error?
This code causes a vertical overflow error when run. Why?
Flutter
Column(
  children: [
    Text('Header'),
    ListView(
      shrinkWrap: true,
      children: List.generate(50, (index) => Text('Item $index')),
    ),
  ],
)
AText widgets inside ListView cause overflow because they are too large.
BListView inside Column has infinite height and causes overflow because it is not wrapped in a widget with fixed height.
CColumn cannot have more than 10 children, so overflow occurs.
DListView must be replaced with ListView.builder to avoid overflow.
Attempts:
2 left
💡 Hint
ListView tries to expand infinitely inside a Column without height constraints.