json - Flutter中解析Json列表
问题描述
如何在 Flutter 的 ListTile>FutureBuilder 中解析这个?我想在 ListTile 中显示 ListTile 标题应该只是 docType。下面是我的url响应模型类。我通过我得到的响应从在线转换器工具生成了这个模型类
//要解析这个JSON数据,做
import 'dart:convert';
final truckDocuments = truckDocumentsFromJson(jsonString);
List<TruckDocuments> truckDocumentsFromJson(String str) =>
List<TruckDocuments>.from(
json.decode(str).map((x) => TruckDocuments.fromJson(x)));
String truckDocumentsToJson(List<TruckDocuments> data) =>
json.encode(List<dynamic>.from(data.map((x) => x.toJson())));
class TruckDocuments {
TruckDocuments({
this.id,
this.docType,
this.docFor,
this.expDate,
this.expMonth,
this.sortOrder,
this.required,
this.status,
this.alert,
this.hasExpiryDate,
this.selected,
});
int id;
String docType;
String docFor;
int expDate;
int expMonth;
int sortOrder;
bool required;
int status;
String alert;
bool hasExpiryDate;
bool selected;
factory TruckDocuments.fromJson(Map<String, dynamic> json) => TruckDocuments(
id: json["id"],
docType: json["docType"],
docFor: json["docFor"],
expDate: json["expDate"],
expMonth: json["expMonth"],
sortOrder: json["sortOrder"],
required: json["required"],
status: json["status"],
alert: json["alert"],
hasExpiryDate: json["hasExpiryDate"],
selected: json["selected"],
);
Map<String, dynamic> toJson() => {
"id": id,
"docType": docType,
"docFor": docFor,
"expDate": expDate,
"expMonth": expMonth,
"sortOrder": sortOrder,
"required": required,
"status": status,
"alert": alert,
"hasExpiryDate": hasExpiryDate,
"selected": selected,
};
}
解决方案
不确定,但这可能对你有用。类 ListViewOfTrack 扩展 StatelessWidget { const ListViewOfTrack({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
body: FutureBuilder(
future: Future.delayed(Duration(seconds: 1)), //Retrieving data
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasData) {
//snapshot contains json data. Pass that to turckDocumentsFromJson
var response = snapshot.data;
final truckDocuments = truckDocumentsFromJson(response.body);
return ListView.builder(
itemBuilder: (context, index) {
return ListTile(
title: truckDocuments[index].docType,
);
},
);
}
return Center(
child: CircularProgressIndicator(),
);
},
),
);
}
}
推荐阅读
- c - 这将是创建的正确数量的子进程吗?
- c# - IQueryable 的顺序是否保留在 C# EF Core 中的实际查询中?
- jhipster - JHipster 使用 jdl 导入和更改现有实体破坏流基验证
- html - 响应式填充/宽度取决于内容
- android - 在 Android 上使用 Room 递归调用 getDatabase 错误
- python - 如何使用 python 获取匹配信息
- c# - 如何以编程方式为合并单元格重新创建 Microsoft Excel 的单元格尺寸 autofit() 函数?
- sparql - 从维基数据查询欧盟大学超时
- emacs - YASnippet 基于缓冲区/文件名的条件激活
- java - 为什么我收到致命错误并且无法在 android studio 上执行 onClick()?