So, I've seen examples of using the initState
function to load data (http) into a provider for a stateful widget. But if the widget is stateless, I'm not sure what the best approach is. What I've done is the following:
class GamesGrid extends StatelessWidget {
@override
Widget build(BuildContext context) {
final gamesData = Provider.of<GamesProvider>(context);
return FutureBuilder<void>(
future: gamesData.fetchGamesFromBgg(),
builder: (ctx, AsyncSnapshot<void> snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
final games = gamesData.items;
return GridView.builder(
padding: const EdgeInsets.all(10.0),
itemCount: games.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: 3 / 2,
crossAxisSpacing: 10,
mainAxisSpacing: 10),
itemBuilder: (context, idx) => ChangeNotifierProvider.value(
value: games[idx],
child: GameItem()
)
);
} else {
return CircularProgressIndicator();
}
}
);
}
}
I'm just not sure that this is the right way to go about it.