0

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.

2 Answers2

0

Try

In your State

Future<void> get() async{
  res = await http.get(url);
  var json = jsonDecode(res.body);
  objectsJson = json['results'] as List;
  setState((){
    objects = List<Object>.from(objectsJson.map((i) => Object.fromMap(i))).toList();});
}

List<Object> objects = [];


@override
initState(){
 get();
}

Then you can use a Builder instead of FutureBuilder

Builder(
 builder: (context){
   if(objects.isEmpty){
      return Container();
   }
   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();
 }
)
Josteve
  • 11,459
  • 1
  • 23
  • 35
0

funny enough i just found the solution. i changed up my fromJson function to using the this.variableName to do the assigning of values

Object.fromJson(Map json) {
      this.id=json['order']['id'];
      this.customer=json['order']['customer'];
      this.receiverName=json['order']['receiver_name'].toString();
      this.receiverPhoneNumber=json['order']['receiver_phone_number'].toString();
      this.pickedUpTime=json['pickedup_time'].toString();
      this.deliveredTime=json['delivered_time'].toString();
  }