Flutter没有返回api调用

时间:2019-10-16 23:10:36

标签: json api flutter

所以我试图用http解析一些json来解析

Future<bool> fetchUser(username) async {
    setLoading(true);

    await Osu(username).fetchUser().then((data) {
      setLoading(false);

      if (data.statusCode == 200) {
        setUser(UserInfo.fromJson(json.decode(data.body)));
      } else {
        print(data.body);
        Map<String, dynamic> result = json.decode(data.body);
        setMessage(result['message']);
      }
    });
    return isUser();
}




class UserInfo {
    String userId;
    String username;
    DateTime joinDate;
    String count300;
    String count100;
    String count50;
    String playcount;
    String rankedScore;
    String totalScore;
    String ppRank;
    String level;
    String ppRaw;
    String accuracy;
    String countRankSs;
    String countRankSsh;
    String countRankS;
    String countRankSh;
    String countRankA;
    String country;
    String totalSecondsPlayed;
    String ppCountryRank;
    List<dynamic> events;

    UserInfo({
        this.userId,
        this.username,
        this.joinDate,
        this.count300,
        this.count100,
        this.count50,
        this.playcount,
        this.rankedScore,
        this.totalScore,
        this.ppRank,
        this.level,
        this.ppRaw,
        this.accuracy,
        this.countRankSs,
        this.countRankSsh,
        this.countRankS,
        this.countRankSh,
        this.countRankA,
        this.country,
        this.totalSecondsPlayed,
        this.ppCountryRank,
        this.events,
    });

Map toJson() => {
    "user_id": userId == null ? null : userId,
    "username": username == null ? null : username,
    "join_date": joinDate == null ? null : joinDate.toIso8601String(),
    "count300": count300 == null ? null : count300,
    "count100": count100 == null ? null : count100,
    "count50": count50 == null ? null : count50,
    "playcount": playcount == null ? null : playcount,
    "ranked_score": rankedScore == null ? null : rankedScore,
    "total_score": totalScore == null ? null : totalScore,
    "pp_rank": ppRank == null ? null : ppRank,
    "level": level == null ? null : level,
    "pp_raw": ppRaw == null ? null : ppRaw,
    "accuracy": accuracy == null ? null : accuracy,
    "count_rank_ss": countRankSs == null ? null : countRankSs,
    "count_rank_ssh": countRankSsh == null ? null : countRankSsh,
    "count_rank_s": countRankS == null ? null : countRankS,
    "count_rank_sh": countRankSh == null ? null : countRankSh,
    "count_rank_a": countRankA == null ? null : countRankA,
    "country": country == null ? null : country,
    "total_seconds_played":
        totalSecondsPlayed == null ? null : totalSecondsPlayed,
    "pp_country_rank": ppCountryRank == null ? null : ppCountryRank,
    "events":
        events == null ? null : List<dynamic>.from(events.map((x) => x)),
};

UserInfo.fromJson(Map json)
  : userId = json["user_id"] == null ? null : json["user_id"],
    username = json["username"] == null ? null : json["username"],
    joinDate = json["join_date"] == null
        ? null
        : DateTime.parse(json["join_date"]),
    count300 = json["count300"] == null ? null : json["count300"],
    count100 = json["count100"] == null ? null : json["count100"],
    count50 = json["count50"] == null ? null : json["count50"],
    playcount = json["playcount"] == null ? null : json["playcount"],
    rankedScore =
        json["ranked_score"] == null ? null : json["ranked_score"],
    totalScore = json["total_score"] == null ? null : json["total_score"],
    ppRank = json["pp_rank"] == null ? null : json["pp_rank"],
    level = json["level"] == null ? null : json["level"],
    ppRaw = json["pp_raw"] == null ? null : json["pp_raw"],
    accuracy = json["accuracy"] == null ? null : json["accuracy"],
    countRankSs =
        json["count_rank_ss"] == null ? null : json["count_rank_ss"],
    countRankSsh =
        json["count_rank_ssh"] == null ? null : json["count_rank_ssh"],
    countRankS = json["count_rank_s"] == null ? null : json["count_rank_s"],
    countRankSh =
        json["count_rank_sh"] == null ? null : json["count_rank_sh"],
    countRankA = json["count_rank_a"] == null ? null : json["count_rank_a"],
    country = json["country"] == null ? null : json["country"],
    totalSecondsPlayed = json["total_seconds_played"] == null
        ? null
        : json["total_seconds_played"],
    ppCountryRank =
        json["pp_country_rank"] == null ? null : json["pp_country_rank"],
    events = json["events"] == null
        ? null
        : List<dynamic>.from(json["events"].map((x) => x));
}

然后这是我的请求呼叫:

import 'package:http/http.dart' as http;

class Osu {
    final String userName;
    final String url = 'https://osu.ppy.sh/api';
    static String apiKey = 'wewewewewwweojjfoejfe';

    Osu(this.userName);

    Future<http.Response> fetchUser() {
        return http.get(url + '/get_user' + "?u=$userName" + "&k=$apiKey");
    }
}

我尝试将其返回到列表中,但我只是得到了相同的错误和建议?

我得到的错误是:

  

TypeError(类型“列表”不是“地图”类型的子类型)

在线

await Osu(username).fetchUser().then((data) {

谢谢!

我不确定它是否映射不正确或我是否弄乱了通话本身。无论哪种方式,在过去的30m里我都对此感到非常困惑,似乎无法解决。 更新为json:

[
{
"user_id": "9795284",
"username": "SakuraMotion",
"join_date": "2017-02-24 17:41:13",
"count300": "4155343",
"count100": "655286",
"count50": "64939",
"playcount": "25758",
"ranked_score": "8433289241",
"total_score": "20762480956",
"pp_rank": "84925",
"level": "99.3314",
"pp_raw": "3318.78",
"accuracy": "95.20140075683594",
"count_rank_ss": "5",
"count_rank_ssh": "0",
"count_rank_s": "473",
"count_rank_sh": "1",
"count_rank_a": "942",
"country": "US",
"total_seconds_played": "1467366",
"pp_country_rank": "14317",
"events": []
}

已更新为data.body响应:

[
  {
    "user_id": "9795284",
    "username": "SakuraMotion",
    "join_date": "2017-02-24 17:41:13",
    "count300": "4185533",
    "count100": "658232",
    "count50": "65063",
    "playcount": "26008",
    "ranked_score": "8509686083",
    "total_score": "20977676803",
    "pp_rank": "84295",
    "level": "99.3547",
    "pp_raw": "3333.68",
    "accuracy": "95.1374740600586",
    "count_rank_ss": "5",
    "count_rank_ssh": "0",
    "count_rank_s": "477",
    "count_rank_sh": "1",
    "count_rank_a": "950",
    "country": "US",
    "total_seconds_played": "1475942",
    "pp_country_rank": "14224",
    "events": [
      {
        "display_html": "<img src='/images/C_small.png'/> <b><a href='/u/9795284'>SakuraMotion</a></b> achieved rank #842 on <a href='/b/1771455?m=3'>Cranky vs. MASAKI - ouroboros -twin stroke of the end- [4K CS' Normal]</a> (osu!mania)",
        "beatmap_id": "1771455",
        "beatmapset_id": "845135",
        "date": "2019-10-19 01:24:15",
        "epicfactor": "1"
      },
      {
        "display_html": "<img src='/images/B_small.png'/> <b><a href='/u/9795284'>SakuraMotion</a></b> achieved rank #812 on <a href='/b/2115037?m=0'>Kousaka Honoka (CV: Nitta Emi) - Snow halation (HONOKA Mix) [Devotion]</a> (osu!)",
        "beatmap_id": "2115037",
        "beatmapset_id": "982344",
        "date": "2019-10-19 01:08:44",
        "epicfactor": "1"
      },
      {
        "display_html": "<img src='/images/B_small.png'/> <b><a href='/u/9795284'>SakuraMotion</a></b> achieved rank #377 on <a href='/b/2173646?m=0'>Roselia - Charles [Expert]</a> (osu!)",
        "beatmap_id": "2173646",
        "beatmapset_id": "1032239",
        "date": "2019-10-19 01:05:46",
        "epicfactor": "1"
      },
      {
        "display_html": "<img src='/images/B_small.png'/> <b><a href='/u/9795284'>SakuraMotion</a></b> achieved rank #70 on <a href='/b/2123647?m=0'>Reol - Jitter Doll [Extra]</a> (osu!)",
        "beatmap_id": "2123647",
        "beatmapset_id": "1010993",
        "date": "2019-10-19 01:02:50",
        "epicfactor": "1"
      }
    ]
  }
]

3 个答案:

答案 0 :(得分:0)

请注意该错误,因为它清楚地表明了您的问题:type 'List<dynamic>' is not a subtype of type 'Map,不确定该错误是否确实在获取中,但

查找https://osu.ppy.sh/api/get_user API的文档并确认响应。您还可以执行以下操作来测试命令行:

curl -X GET https://osu.ppy.sh/api/get_user&u=USERNAME&k=KEY 并确保您获得200个正确的结构。

答案 1 :(得分:0)

我认为错误在于此行

Map<String, dynamic> result = json.decode(data.body);

json.decode(data.body)的结果实际上是一个列表,但您已将其分配给Map

我认为您可以将其更改为

List<Dynamic> result = json.decode(data.body);

List result = json.decode(data.body);

如果您确实需要进行映射,则可以稍后从列表中进行映射

答案 2 :(得分:0)

有很多原因可能会导致您收到错误消息……提供API返回的JSON示例将是有益的。

您可以检查您的UserInfo()...它具有以下字段:

 List<dynamic> events;

再次检查来自JSON的事件是否真的是一个列表。

如果您在解析复杂的JSON文件时遇到问题,最好使用JsonSerializable https://flutter.dev/docs/development/data-and-backend/json#creating-model-classes-the-json_serializable-way