I have a json of structure
{
"message": "",
"status" : "complete",
"results": [
{
"id": 66,
"user": {
"id": 80,
"email": "+fsefs@gmail.com"
},
"order": {
"id": 1,
"customer": 2,
"receiver_name": "Something",
"receiver_phone_number": "464"
},
"order": {
"id": 1,
"customer": 2,
"receiver_name": "Something",
"receiver_phone_number": "464"
},
"pickedup_time": "sksjkns",
"delivered_time": "hkvjsfsf"
}
]
}
and i am parsing the json this way and also trying to map it into a list of objects
Future<void> get() async{
res = await http.get(url);
var json = jsonDecode(res.body);
objectsJson = json['results'] as List;
objects = List<Object>.from(objectsJson.map((i) => Object.fromMap(i))).toList();
}
where res,objectsJson,objects are a global variables declared as
var res;
List<Object> objects;
var objectsJson;
and this is my class for Object with the method fromMap that i use to create the classes
class Object{
int id, customer;
String
receiverName,
receiverPhoneNumber,
pickedUpTime,
deliveredTime;
Object(
{
int id,
int customer,
String receiverName,
String receiverPhoneNumber,
String pickedUpTime,
String deliveredTime
}
);
factory Object.fromMap(Map<String, dynamic> json) {
print(json['order']['id'].toString()+
json['order']['customer'].toString()+
json['order']['receiver_name'].toString()+
json['order']['receiver_phone_number'].toString()+
json['pickedup_time'].toString()+
json['delivered_time'].toString()
);
return new Object(
id: json['order']['id'],
customer: json['order']['customer'],
receiverName: json['order']['receiver_name'],
receiverPhoneNumber: json['order']['receiver_phone_number'],
pickedUpTime: json['pickedup_time'],
deliveredTime: json['delivered_time']
);
}
}
as you can see i printed the values out before creating the object and it displayed correctly. but when i try to access the object from the list of objects as below from a futurebuilder i get null values throughout.
FutureBuilder(
future: get(),
build: (BuildContext context, AsyncSnapshot snapshot){
if(snapshot.connectionState == ConnectionState.done){
print(objects[0].id);
print(objects[0].customer);
print(objects[0].receiverName);
print(objects[0].receiverPhoneNumber);
print(objects[0].pickedUpTime);
print(objects[0].receivdeliveredTimeerName);
return Container();
}
return Container();
}
}
i have printed out the json and it is intact and that can be backed by not getting null but the actual values when i print out the values before returning the object in the class.
i have tried printing out objects after mapping and this is what i get
['Instance of Object']
i have also tried adding .toString() to the ones that are strings in returning the object. For example:
receiverName: json['order']['receiver_name'].toString()
I dont know where the null is coming from.