【问题标题】:Flutter is not returning an api callFlutter 没有返回 api 调用
【发布时间】:2020-02-13 19:04:43
【问题描述】:

所以我试图用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(类型'List'不是类型'Map'的子类型)

上线

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"
      }
    ]
  }
]

【问题讨论】:

  • 嗨,泰勒。您可以在拨打电话时向我们提供结果 JSON 的示例吗?它会为我们提供一些关于似乎是什么问题的线索。
  • 如果您使用的是await,请不要使用then,例如:var data = await Osu(username).fetchUser();
  • 嗨,对不起,当你们回复时我正在睡觉,我已经用我期待返回的 json 更新了 OP
  • 是的,我遇到了同样的问题。
  • 这是我添加imgur.com/a/b1RNjIa时得到的结果

标签: json api flutter


【解决方案1】:

注意错误,因为它清楚地说明了您的问题:type 'List&lt;dynamic&gt;' 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&amp;u=USERNAME&amp;k=KEY 并确保您获得 200 个正确的结构。

【讨论】:

    【解决方案2】:

    我认为错误在这一行

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

    json.decode(data.body) 的结果实际上是一个 List 但你将它分配给了 Map

    我觉得你可以改成

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

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

    如果你真的需要映射它,你可以稍后从列表中映射它

    【讨论】:

      【解决方案3】:

      您可能会收到错误的原因有很多...提供 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

      【讨论】:

        猜你喜欢
        • 2020-07-02
        • 2014-04-30
        • 2018-11-01
        • 1970-01-01
        • 2020-02-22
        • 2021-12-18
        • 2012-09-02
        • 2020-03-27
        • 1970-01-01
        相关资源
        最近更新 更多