im relatively new to flutter. I'm using an async future and a future building to populate a widget with the returned data which is in a nested object. The problem I'm having is to access the nested list of objects inside my object. I keep getting [Instance of 'License', Instance of 'License', Instance of 'License', Instance of 'License', Instance of 'License']
when i log snapshot.data!.license
instead of json?, and when i try to creating a listview the output is empty.
My view looks like this:
body: SingleChildScrollView(
child: new FutureBuilder<Profile>(
future: _license,
builder: (context, snapshot) {
if (snapshot.hasData == false || snapshot.data == null) {
return CircularProgressIndicator();
} else if (snapshot.hasError) {
return Text("${snapshot.error}");
} else {
var x = snapshot.data!.license;
x.map((e) {
print(e);
return Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(e.fromDate.toString()),
])
]);
});
}
return const Center(child: CircularProgressIndicator());
})
)
And my object looks like this :
{
"id": "1",
"license": [
{
"license_no": "229451",
"from_date": "2020-11-12",
"to_date": "2021-11-30",
"workStation": "Johpas Clinic"
},
{
"license_no": "189024",
"from_date": "2019-11-04",
"to_date": "2020-11-30",
"workStation": null
},
{
"license_no": "109872",
"from_date": "2016-11-30",
"to_date": "2019-11-30",
"workStation": null
},
{
"license_no": "066356",
"from_date": "2013-11-30",
"to_date": "2016-11-30",
"workStation": null
},
{
"license_no": "007853 ",
"from_date": "2010-11-30",
"to_date": "2013-11-30",
"workStation": null
}
]
}
CodePudding user response:
You can create a toJson
method in the License
class and then use that for logging purposes, or override the toString
method that is added to every class created in dart.
class License {
String id;
License(this.id);
Map<String, dynamic> toJson() => {
'id' : id,
};
CodePudding user response:
I used indexes to access the data
} else if(snapshot.hasData) {
Profile? data = snapshot.data;
return ListView.builder(
itemCount: data!.length,
shrinkWrap: true,
primary: false,
itemBuilder: (BuildContext context, int index) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
16.height,
Text(("Education History").validate(), style: boldTextStyle()),
16.height,
// child: Text(data.license[index].fromDate.toString()),
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
16.width,
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text((data.registration[index].cadre).toString(),
style: boldTextStyle(
size: 14, color: blackColor)),
8.height,
Text(
(data.registration[index].cadreText.toString())
.validate(),
style: boldTextStyle(
size: 13, color: Colors.green)),
8.height,
Text(
(data.registration[index].regNo
.toString())
.validate(),
style: boldTextStyle(
size: 12, color: mlColorRed)),
],
).expand()
],
).paddingBottom(16.0),
],
).paddingAll(16.0);
}
);
}