I'm looking to get a list from a Future and to use this list in a ListView.Builder. Only problem is I'm obviously finding a good way to accomplish this. Here's some code snippets if anyone can offer me some advice. Thanks!
Where I call the function for the Data and also the ListView.Builder
class _ChangeQuarterState extends State<ChangeQuarter> {
static const Color greycolor = Color.fromRGBO(220, 220, 220, 10);
Future<List<Quarter>> quarters = getQuarters();
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey[200],
appBar: PreferredSize(
preferredSize: Size.fromHeight(95.0),
child: AppBar(
automaticallyImplyLeading: false, // hides leading widget
flexibleSpace: QuarterAppBar(),
),
),
body: Container(
child: ListView.builder(
itemCount: quarters[index],
shrinkWrap: true,
itemBuilder: (context, index) {
return Padding(
padding: EdgeInsets.symmetric(vertical: 1.0, horizontal: 4.0),
child: Card(
color: (index % 2 == 0) ? greycolor : Colors.white,
child: Container(
height: 60,
padding: EdgeInsets.fromLTRB(0, 20, 0, 0),
child: InkWell(
onTap: () => Navigator.pop(context),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
margin: EdgeInsets.only(right: 5),
child: Text(quarters[index].quarter,
style: TextStyle(
fontSize: 20,
fontFamily: 'Montserrat',
color: BBFILightBlue),
textAlign: TextAlign.left),
),
Where I get the data I want to turn into a list for the Builder
Future<Quarter> getQuarters() async {
final http.Response response = await http.get(
'https://fakedapi.com/getQuarters',
headers: <String, String>{
'Authorization': 'topsecret',
},
);
if (response.statusCode <400) {
return Quarter.fromJson(json.decode(response.body));
} else {
throw Exception('Failed to get quarters');
}
}