由于某種原因,我遇到了問題,在運行應用程式時,api 回應中列出的所有 cands(物件串列)都不會顯示在 ListView 中。這是我從 api 得到的回應:
{
"currentPage": 1,
"totalPages": 1,
"totalResults": 4, // that's the amount of events in the list
"candList": [
{
"fname": "string",
"lname": "string"
}
]
}
我猜這個問題從那部分代碼的某個地方開始:
final num = getJsonField(listViewGetCandListByManagerResponse.jsonBody, r'''$.candList''',).toList();
if (num.isEmpty) {
return Center(
child: SworkerContainerWidget(
fname: GetCandListByManagerCall.workerfname(listViewGetCandListByManagerResponse.jsonBody,).toString(),
lname: GetCandListByManagerCall.workerlname(listViewGetCandListByManagerResponse.jsonBody,).toString(),),);
}
return ListView.builder(
padding: EdgeInsets.zero,
scrollDirection: Axis.vertical,
itemCount: num.length,
itemBuilder: (context, numIndex) {
final numItem = num[numIndex];
return InkWell(
onTap: () async {
await Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
HistoryPageWidget(),
),
);
},
child: SworkerContainerWidget(
desc: GetCandListByManagerCall
.workereventDesc(
listViewGetCandListByManagerResponse
.jsonBody,
).toString(),
fname: GetCandListByManagerCall.workerfname(
listViewGetCandListByManagerResponse
.jsonBody,
).toString(),
lname: GetCandListByManagerCall.workerlname(
listViewGetCandListByManagerResponse
.jsonBody,
).toString(),
dateEvent:
GetCandListByManagerCall.dateEvent(
listViewGetCandListByManagerResponse
.jsonBody,
).toString(),
),
);
},
);
我也可能錯了,但請告訴我。我在運行應用程式時得到的回應:

dynamic getJsonField(dynamic response, String jsonPath) {
final field = JsonPath(JsonPath).read(response);
return field.isNotEmpty
? field.length > 1
? field.map((f) => f.value).toList() : field.first.value : null;
}
class GetCandListByManagerCall {
static Future<ApiCallResponse> call({int? entityId}) {
final body = {
"token": "", //deleted in the post specifically
"entityId": ${entityId},
"pageNumber": 1,
"rowsPerPage": 10,
"search": ""
};
return Apimanager.instance.makeApicall(callName:'GetCandListByManager',....);
} //the other parts in this line arent really helpful and have private info
static dynamic workerfname(dynamic response) => getJsonField(response, r'''$.fname''',);
static dynamic workerlname(dynamic response) => getJsonField(response, r'''$.lname''',);
}
如果有任何缺失的資訊可以更深入地了解問題,請告訴我,我會提供。
uj5u.com熱心網友回復:
首先創建這樣的類模型:
class CandModel {
final String firstName;
final String lastName;
const CandModel({
required this.firstName,
required this.lastName,
});
static CandModel fromJson(Map<String, dynamic> json) {
return CandModel(
firstName: json['fname'],
lastName: json['lname'],
);
}
}
然后像這樣決議你的api,而不是這樣:
final num = getJsonField(listViewGetCandListByManagerResponse.jsonBody, r'''$.candList''',).toList();
用這個:
final data = listViewGetCandListByManagerResponse.jsonBody['candList'] as List;
List<CandModel> cands = data.map((e) => CandModel.fromJson(e)).toList();
然后在您的串列視圖中使用它,如下所示:
SworkerContainerWidget(
desc: GetCandListByManagerCall.workereventDesc(
listViewGetCandListByManagerResponse.jsonBody).toString(),
fname: cands[index].firstName,
lname: cands[index].lastName,,
dateEvent:GetCandListByManagerCall.dateEvent(
listViewGetCandListByManagerResponse.jsonBody).toString(),
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/506372.html
