So I'm new to Flutter and I keep running into the same issue.
Future<List<DonationDTO>> getUsersAppointments() async {
List<DonationDTO> AppointmentRes = [];
AppointmentRes = (await _apiClient.getDonationsOfUser(widget.user.id, widget.accesstoken));
return AppointmentRes;
}
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
appBar: AppBar(
backgroundColor: Colors.pink,
elevation: .0,
leading: BackButton(
onPressed: () => Navigator.pop(context),
color: Colors.white),
title: Text("text"),
),
body:SingleChildScrollView(
child: FutureBuilder<List<DonationDTO>>(
future: getUsersAppointments(),
builder: (context, AsyncSnapshot snapshot) {
if (!snapshot.hasData || snapshot.hasError) {
return new Container(child:CircularProgressIndicator());
} else {
return Container(
child: ListView.builder(
itemCount: snapshot.data.length,
scrollDirection: Axis.horizontal,
itemBuilder: (BuildContext context, int index) {
if (!snapshot.data[index].hasData || snapshot.data[index].hasError) {
return new Container(child:CircularProgressIndicator());
} else {
return Text('${snapshot.data[index].id}');}
}));
}
}
),
),
),
);
}
The problem with this widget is that while using the ListView I keep getting a null value error, more specifically when I try to get data from snapshot.data[index].
It also appears that when I display the content of snapshot.data I get this: [Instance of 'DonationDTO', Instance of 'DonationDTO']
I don't know how to fix this, so any help would be appreciated. Thanks!