Flutter:将JSON映射到对象列表将返回null

问题描述

我有一个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"
        },"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;

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

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中访问对象列表时,如下所示,整个过程中我都得到空值。

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而不是null来支持,而当我在返回类中的对象之前打印出这些值时可以返回实际值。

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

['Instance of Object']

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

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

我不知道null的来源。

解决方法

尝试

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

然后,您可以使用Builder代替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();
 }
)
,

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

var choice3;