flutter - 在 Flutter(Dart) 中访问嵌套实例对象中的数据
问题描述
我比较陌生。我正在使用异步未来和未来构建来使用嵌套对象中的返回数据填充小部件。我遇到的问题是访问对象内的嵌套对象列表。[Instance of 'License', Instance of 'License', Instance of 'License', Instance of 'License', Instance of 'License']
当我登录而不是 json? 时,我不断得到snapshot.data!.license
,当我尝试创建列表视图时,输出为空。我的观点是这样的:
body: SingleChildScrollView(
child: new FutureBuilder<Profile>(
future: _license,
builder: (context, snapshot) {
if (snapshot.hasData == false || snapshot.data == null) {
return CircularProgressIndicator();
} else if (snapshot.hasError) {
return Text("${snapshot.error}");
} else {
var x = snapshot.data!.license;
x.map((e) {
print(e);
return Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(e.fromDate.toString()),
])
]);
});
}
return const Center(child: CircularProgressIndicator());
})
)
我的对象看起来像这样:
{
"id": "1",
"license": [
{
"license_no": "229451",
"from_date": "2020-11-12",
"to_date": "2021-11-30",
"workStation": "Johpas Clinic"
},
{
"license_no": "189024",
"from_date": "2019-11-04",
"to_date": "2020-11-30",
"workStation": null
},
{
"license_no": "109872",
"from_date": "2016-11-30",
"to_date": "2019-11-30",
"workStation": null
},
{
"license_no": "066356",
"from_date": "2013-11-30",
"to_date": "2016-11-30",
"workStation": null
},
{
"license_no": "007853 ",
"from_date": "2010-11-30",
"to_date": "2013-11-30",
"workStation": null
}
]
}
解决方案
您可以在类中创建一个toJson
方法,License
然后将其用于日志记录目的,或者覆盖toString
添加到 dart 中创建的每个类的方法。
class License {
String id;
License(this.id);
Map<String, dynamic> toJson() => {
'id' : id,
};
推荐阅读
- java - RestAssured:无法添加 Content-Length 标头,失败并显示它已经存在的消息
- vba - 为什么有时使用 Instr 函数会出现错误 5?
- timestamp - firebase 按时间戳过滤
- ios - 如果 let 不解开 MKAnnotation 的 title 属性的可选值
- ros - 如何查看点云?
- javascript - 计算过滤列的总和
- matlab - 在 MATLAB 中使用彩色图像的四元数加权平均滤波器
- dynamics-crm-online - 完整复制和还原备份之间的区别
- android - Google 日历 API 私人活动
- javascript - 我对 Focus 事件和 Blur 事件有疑问