我想在listview中显示玩家的统计数据,我正在使用这个api:https://cricapi.com/api/playerStats?apikey=apikey&pid=pid
上述api的输出如下:
{
"pid": xxxx,
"profile": "profile description",
"imageURL": "https://www.cricapi.com/playerpic/xxxx.jpg",每个播放器的pid都从另一个api中检索:
https://cricapi.com/api/playerFinder?apikey=apikey&name=playerName
上述api的输出如下:
{
"data": [
{
"pid": xxxx,
"fullName": "Firstname Lastname",目前,我正在传递第一个api中的硬编码pid,以显示播放器的统计数据和代码:
FetchJson() async {
var response = await http.get(
'https://cricapi.com/api/playerStats?apikey=apikey&pid=1111');
if (response.statusCode == 200) {
String responseBody = response.body;
var responseJson = jsonDecode(responseBody);
pid = responseJson['pid'];
name = responseJson['name'];
playingRole = responseJson['playingRole'];
battingStyle = responseJson['battingStyle'];
country = responseJson['country'];
imageURL = responseJson['imageURL'];
data = responseJson;
var stats = data['data']['batting'];
var testStats = stats['tests'];
var odiStats = stats['ODIs'];
var tStats = stats['T20Is'];
// T20 Stats
matches_t = tStats['Mat'];
runs_t = tStats['Runs'];
half_t = tStats['50'];
century_t = tStats['100'];
highest_t = tStats['HS'];
avg_t = tStats['Ave'];我打电话给FetchJson() I initState()。
我尝试了类似的/先前的问题How to fetch api data by passing variables (parameters)?的解决方案,但这使我走上了一条不同的道路。我无法实现该解决方案,因为我无法通过pid接收到的第一个api返回FetchJson()。
我的问题是:
如何从第二个api (playerFinder)中检索playerFinder并将其提供给第一个api (playerStats),以及如何使用该pid来代替传递硬编码的pid,我可以将pid作为变量传递,并在UI中显示多个参与者的统计信息?
必需的代码在这里:https://pastebin.com/iU8x9U8z
我想在UI中显示玩家的统计数据,但目前我正在通过硬编码的playerid,它只显示一个玩家的统计数据,但我想显示不同的球员统计数据。
*更新*
作为另一种解决方案,我现在使用pids列表,并解析那些使用map并将它们传递到for循环中的FetchJson(),如下所示:
var playerIds = [{"pid":35320},{"pid":28114},{"pid":28779},{"pid":28763},{"pid":30176},{"pid":7133},{"pid":5390}]
@override
void initState() {
var intIds = playerIds.map<int>((m) => m['pid'] as int).toList();
for (int i = 0; i < intIds.length; i++) {
FetchJson(intIds[i]);
}
}
FetchJson(int ids) async {
print(ids);
var response = await http.get(
'https://cricapi.com/api/playerStats?apikey=apikey&pid=$ids');
....
}我现在面临的问题是,它从列表中获取最后一个pid,并在UI中反复显示其数据。我希望看到的预期输出是: UI中所有pids的播放器数据,我不知道如何实现这一点。
完整的参考代码在这里:https://pastebin.com/kFYBfHuf
发布于 2018-12-27 19:33:29
一个解决方案是从这两组api创建Maps,直到需要的播放器数据,然后使用下面编写的类似于where子句的开关语句来识别匹配的数据。
最大的问题是,您需要在两个api中识别匹配的数据项。在我的示例中,我假设它可能是一个球员的名字,也可能是他们的团队和团队号,但是必须有一些东西来验证您正在查看同一名球员的不同数据点。
switch(variable_expression) {
case name = full_name: {
// statements;
}
break;
case constant_expr2: {
//statements;
}
break;
default: {
//statements;
}
break;
} https://stackoverflow.com/questions/53949314
复制相似问题