0

我有一个结构的 json

{
  "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"
      }
    ]
  }

我正在以这种方式解析 json 并尝试将其映射到对象列表中

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();
}

其中 res,objectsJson,objects 是声明为的全局变量

var res;
List<Object> objects;
var objectsJson;

这是我用来创建类的方法 fromMap 的 Object 类

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']
        );
  }
}

如您所见,我在创建对象之前打印了这些值,并且它正确显示。但是当我尝试从如下的对象列表中访问对象时,我会在整个过程中得到空值。

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();
  }

}

我已经打印出 json 并且它是完整的,并且当我在返回类中的对象之前打印出值时,可以通过不获取 null 但实际值​​来支持它。

我尝试在映射后打印出对象,这就是我得到的

['Instance of Object']

我还尝试将 .toString() 添加到返回对象的字符串中。例如:

receiverName: json['order']['receiver_name'].toString()

我不知道 null 来自哪里。

4

2 回答 2

0

尝试

在你的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();
}

然后你可以使用 aBuilder而不是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();
 }
)
于 2020-09-01T16:37:01.707 回答
0

很有趣,我刚刚找到了解决方案。我将我的 fromJson 函数更改为使用 this.variableName 来分配值

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();
  }
于 2020-09-01T16:44:08.810 回答