So, I have this "Notifications" screen that displays notifications for the user. When navigating to this screen, it's going to be blank, since the notifications are being loaded live from a backend API.
Here's some code to illustrate the problem:
class _MyItemsPageState extends State<MyItemsPage> {
final GlobalKey<RefreshIndicatorState> _refreshIndicatorKey =
new GlobalKey<RefreshIndicatorState>();
List<MyItem> _items = [];
@override
void initState() {
super.initState();
// Nothing is displaying on screen initially, since the items are loaded from API on startup.
// Preferably in this state, the refresh indicator would be shown while the items load.
// It's not currently possible in this place, since it seems that the Widget hasn't been built yet.
_refreshIndicatorKey.currentState.show(); // currentState null at this time, so the app crashes.
_loadItems();
}
// (unrelated code removed)
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
),
body: new RefreshIndicator(
key: _refreshIndicatorKey,
onRefresh: _loadItems,
child: new ListView(
padding: new EdgeInsets.symmetric(vertical: 8.0),
children: _buildItemWidgets(),
),
),
);
}
}
The problem is that the _refreshIndicator.currentState is null when the initState() function is called, since the Widget hasn't been built yet.
What is the proper place of calling show() on the RefreshIndicator in this case?