Flutter:将 JSON 映射到对象列表中返回 null
Posted
技术标签:
【中文标题】Flutter:将 JSON 映射到对象列表中返回 null【英文标题】:Flutter: Mapping JSON into List of Objects returns null 【发布时间】:2020-12-20 19:17:18 【问题描述】:我有一个 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 的对象列表中访问对象时,我在整个过程中都得到了空值。
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 是从哪里来的。
【问题讨论】:
不要将你的类命名为 'Object' 。对象类是每个类都隐式继承的***类 我没有把它命名为对象。我只是将其更改为反对在此处发布。 【参考方案1】:很有趣,我刚刚找到了解决方案。 我将我的 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();
【讨论】:
【参考方案2】:试试
在你的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();
)
【讨论】:
我也试过这个。这与更新 UI 无关,而是对象中的实际值,以上是关于Flutter:将 JSON 映射到对象列表中返回 null的主要内容,如果未能解决你的问题,请参考以下文章
如何使用从 Flutter 中的 json 解析的嵌套映射列表中的函数创建对象