Sub topic 13
Pull to Refresh in Flutter
This chapter assumes basic Dart, Flutter widgets, StatefulWidget, ListView.builder, and asynchronous functions. Pull to refresh lets a user drag a list downward to request newer data. Flutter provides this behavior through RefreshIndicator.
How `RefreshIndicator` works
RefreshIndicator wraps a vertical scrollable, such as ListView. When the user drags from the top far enough, Flutter displays a progress indicator and calls onRefresh.
The callback must return a Future. Keep the future incomplete while the refresh work runs. Flutter hides the indicator after the future completes.
A complete example
This example simulates a network request by waiting for one second. A real application would replace _loadMessages with an HTTP or database operation.
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: MessagesPage()));
}
class MessagesPage extends StatefulWidget {
const MessagesPage({super.key});
@override
State<MessagesPage> createState() => _MessagesPageState();
}
class _MessagesPageState extends State<MessagesPage> {
List<String> _messages = ['Welcome', 'Your report is ready'];
Future<void> _loadMessages() async {
await Future<void>.delayed(const Duration(seconds: 1));
if (!mounted) return;
setState(() {
_messages = [
'Updated at ${DateTime.now().toLocal()}',
'Welcome',
'Your report is ready',
];
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Messages')),
body: RefreshIndicator(
onRefresh: _loadMessages,
child: ListView.builder(
physics: const AlwaysScrollableScrollPhysics(),
itemCount: _messages.length,
itemBuilder: (context, index) {
return ListTile(title: Text(_messages[index]));
},
),
),
);
}
}AlwaysScrollableScrollPhysics is useful when the list is shorter than the screen. Without it, there may not be enough scrollable content for the user to pull. The property changes the scroll behavior; it does not make the list longer.
The mounted check prevents setState from running if the page was removed while the asynchronous operation was still running.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| The indicator never appears | The child cannot scroll | Use `AlwaysScrollableScrollPhysics` |
| Refresh starts in the wrong scrollable | Nested scroll views send notifications upward | Set an appropriate `notificationPredicate` |
| The indicator disappears too soon | `onRefresh` returns before the request finishes | Return or await the actual `Future` |
| The app reports a layout error | The scrollable has unbounded constraints | Give it a bounded height, often with `Expanded` |
When using nested scrollables, decide which scrollable owns refresh gestures. In a CustomScrollView, use the sliver-based refresh pattern appropriate to the surrounding layout rather than adding unrelated vertical scroll views.
Exercise
Why does the example use AlwaysScrollableScrollPhysics?
Show answer
It allows the user to trigger a downward drag even when the list has too few items to overflow the viewport.