0
0
Fluttermobile~20 mins

GridView.builder in Flutter - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
GridView Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What is the output of this GridView.builder code?

Consider this Flutter code snippet that builds a grid of colored squares. What will you see on the screen?

Flutter
GridView.builder(
  gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 3),
  itemCount: 6,
  itemBuilder: (context, index) {
    return Container(
      color: index % 2 == 0 ? Colors.red : Colors.blue,
      margin: EdgeInsets.all(4),
    );
  },
)
AA single column list of 6 red squares.
BA grid with 2 columns and 3 rows, all squares red.
CA grid with 3 columns and 2 rows, alternating red and blue squares starting with red.
DA grid with 3 columns and 2 rows, all squares blue.
Attempts:
2 left
💡 Hint

Look at the crossAxisCount and the color condition using index % 2 == 0.

lifecycle
intermediate
1:30remaining
When does the itemBuilder function get called in GridView.builder?

In Flutter's GridView.builder, when is the itemBuilder function called?

AEvery time an item scrolls into view, to build that item widget lazily.
BAfter the grid is fully rendered, to update all items.
COnly for the first visible row of items.
DOnly once for all items when the grid is first built.
Attempts:
2 left
💡 Hint

Think about how Flutter optimizes building large lists or grids.

📝 Syntax
advanced
2:00remaining
Which option correctly sets a fixed aspect ratio for grid items?

You want each grid item to have a width twice its height. Which gridDelegate code achieves this?

ASliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2, childAspectRatio: 2.0)
BSliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2, childAspectRatio: 0.5)
CSliverGridDelegateWithMaxCrossAxisExtent(maxCrossAxisExtent: 100, childAspectRatio: 0.5)
DSliverGridDelegateWithMaxCrossAxisExtent(maxCrossAxisExtent: 100, childAspectRatio: 2.0)
Attempts:
2 left
💡 Hint

Remember childAspectRatio is width divided by height.

🔧 Debug
advanced
2:00remaining
Why does this GridView.builder cause a runtime error?

Look at this code snippet:

GridView.builder(
  gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 3),
  itemCount: 5,
  itemBuilder: (context, index) {
    if (index == 4) return null;
    return Container(color: Colors.green);
  },
)

Why does it cause an error?

ABecause <code>crossAxisCount</code> must be equal to <code>itemCount</code>.
BBecause <code>itemBuilder</code> returned null, which is not allowed.
CBecause <code>itemCount</code> is less than the number of items built.
DBecause <code>Container</code> cannot be used inside <code>GridView.builder</code>.
Attempts:
2 left
💡 Hint

What does itemBuilder expect to return?

🧠 Conceptual
expert
2:30remaining
How does GridView.builder improve performance compared to GridView.count?

Choose the best explanation for why GridView.builder is more efficient than GridView.count for large data sets.

A<code>GridView.builder</code> disables scrolling by default, improving speed.
B<code>GridView.builder</code> uses less memory by caching all items, <code>GridView.count</code> does not cache.
C<code>GridView.builder</code> automatically paginates data, <code>GridView.count</code> does not.
D<code>GridView.builder</code> builds only visible items lazily, while <code>GridView.count</code> builds all items at once.
Attempts:
2 left
💡 Hint

Think about how many widgets are created initially.