Consider a Flutter app with a RefreshIndicator wrapping a ListView. What is the expected behavior when the user pulls down on the list?
RefreshIndicator(
onRefresh: () async {},
child: ListView.builder(
itemCount: 10,
itemBuilder: (context, index) => ListTile(title: Text('Item $index')),
),
)Think about what RefreshIndicator is designed to do when the user pulls down.
The RefreshIndicator widget shows a circular progress indicator when the user pulls down on its child scrollable widget. It then calls the onRefresh callback to allow refreshing data.
In Flutter's RefreshIndicator, what must the onRefresh callback return?
Consider that the refresh action is asynchronous and the indicator waits for it to complete.
The onRefresh callback must return a Future<void> because the RefreshIndicator shows the spinner until the future completes.
Choose the Flutter code snippet that correctly implements a pull-to-refresh feature using RefreshIndicator and updates the list items.
class MyList extends StatefulWidget { @override State<MyList> createState() => _MyListState(); } class _MyListState extends State<MyList> { List<String> items = ['A', 'B', 'C']; Future<void> _refresh() async { await Future.delayed(Duration(seconds: 1)); setState(() { items.add('D'); }); } @override Widget build(BuildContext context) { return RefreshIndicator( onRefresh: _refresh, child: ListView.builder( itemCount: items.length, itemBuilder: (context, index) => ListTile(title: Text(items[index])), ), ); } }
Remember that onRefresh expects a Future and setState updates UI.
The code correctly returns a Future
A developer uses RefreshIndicator but the spinner stays visible forever after pulling down. What is the most likely cause?
RefreshIndicator(
onRefresh: () async {
// Forgot to complete the future
},
child: ListView(...),
)Think about what controls the spinner visibility.
The spinner stays visible because the Future returned by onRefresh never completes. The RefreshIndicator waits for this future to finish before hiding the spinner.
In Flutter, how does RefreshIndicator ensure that pull-to-refresh activates only when the scrollable is at the top?
Think about how scroll position affects user gestures.
The RefreshIndicator internally listens to the scroll controller and only triggers the refresh gesture if the scroll offset is at the top (zero or less), preventing accidental refreshes when scrolled down.