Challenge - 5 Problems
Infinite Scrolling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
Detecting scroll position for infinite scrolling
In Flutter, which code snippet correctly detects when the user has scrolled to the bottom of a ListView to trigger loading more items?
Flutter
ScrollController _controller = ScrollController();
_controller.addListener(() {
if (_controller.position.pixels == _controller.position.maxScrollExtent) {
// Load more items
}
});Attempts:
2 left
💡 Hint
Think about when the scroll position reaches the bottom edge.
✗ Incorrect
The scroll position's pixels equal to maxScrollExtent means the user reached the bottom. Option B checks this exactly. Option B checks if at any edge (top or bottom), which is not specific enough.
❓ lifecycle
intermediate1:30remaining
Properly disposing ScrollController
Which Flutter widget lifecycle method is the best place to dispose a ScrollController to avoid memory leaks?
Attempts:
2 left
💡 Hint
Think about cleaning up resources when the widget is removed.
✗ Incorrect
dispose() is called when the widget is removed permanently. It's the right place to dispose controllers. initState() is for initialization, build() runs often, and didChangeDependencies() is for dependency changes.
📝 Syntax
advanced2:30remaining
Correct async data loading in infinite scroll
Which code snippet correctly loads more data asynchronously when the user scrolls to the bottom, updating the UI in Flutter?
Flutter
void _loadMore() async { final newItems = await fetchItems(); setState(() { items.addAll(newItems); }); } _controller.addListener(() { if (_controller.position.pixels == _controller.position.maxScrollExtent) { _loadMore(); } });
Attempts:
2 left
💡 Hint
UI updates must happen inside setState after data is ready.
✗ Incorrect
Option A correctly waits for data asynchronously, then updates state inside setState. Option A misses await and setState, so UI won't update properly. Option A updates UI before data is ready. Option A uses synchronous fetch which is unrealistic for network calls.
advanced
2:30remaining
Maintaining scroll position after navigation
After navigating back to a screen with infinite scrolling, which approach preserves the scroll position in Flutter?
Attempts:
2 left
💡 Hint
Think about keeping the controller alive to remember scroll offset.
✗ Incorrect
Keeping the same ScrollController preserves scroll offset. Creating a new one resets position. Resetting scroll to zero loses position. GlobalKey rebuilds widget but does not preserve scroll automatically.
🧠 Conceptual
expert3:00remaining
Handling rapid scroll events in infinite scrolling
What is the best way to prevent multiple simultaneous data fetches when the user scrolls quickly to the bottom multiple times in Flutter infinite scrolling?
Attempts:
2 left
💡 Hint
Think about how to avoid starting a new fetch before the previous finishes.
✗ Incorrect
Using a boolean flag (e.g., isLoading) prevents starting multiple fetches simultaneously. Calling fetch every time causes duplicate requests. Clearing items loses data. Delaying fetches arbitrarily can cause bad UX.