我通过分步指南学习颤振。因此,当我使用指南中的代码时 - 它运行良好,但是当我尝试更改 JSON URL 时,出现错误。据我了解,JSON 格式和 jsonDecode 的问题无法解码 RIOT JSON,但如何解决?
List _games = await getGames();
Future<List> getGames() async {
//Not working with this JSON URL
String apiUrl = 'https://euw1.api.riotgames.com/lol/match/v4/matchlists/by-account/UZs5l9TT7GLEPfoOZ1eTMqqyJEomgmUHueGQ2aFNHaYTOZI/?api_key=RGAPI-07972f29-94f8-4d54-a2dd-527d4eeb0335';
//Working good with this JSON URL
String apiUrl = 'https://jsonplaceholder.typicode.com/posts';
http.Response response = await http.get(apiUrl, headers: {"Accept": "application/json"});
print(jsonDecode(response.body));
return jsonDecode(response.body);
}
Run Code Online (Sandbox Code Playgroud)
改变
Future<List>
Run Code Online (Sandbox Code Playgroud)
到
Future<Map<String,dynamic>>
Run Code Online (Sandbox Code Playgroud)
如果你有类似的 JSON [1, 2, 3],或者["foo", "bar", "baz"]你会得到一个List,但是如果你有类似的 JSON,{"foo": 1, "bar": 2, "baz": 3}你会得到一个Map<String,dynamic>. 因为"foo"你会得到 aString和truea bool,等等。我想你明白了。
| 归档时间: |
|
| 查看次数: |
12828 次 |
| 最近记录: |