gpt4 book ai didi

json - Flutter 错误 : Expected a value of type 'FutureOr>' , 但得到了 '_JsonMap' 类型之一

转载 作者:行者123 更新时间:2023-12-04 14:51:06 27 4
gpt4 key购买 nike

当我尝试解码 JSON 时,出现此错误:

Expected a value of type 'FutureOr<List>', but got one of type '_JsonMap'


这是我从 API 获取数据的代码:
Future<List> getData() async {
final prefs = await SharedPreferences.getInstance();
final key = 'token';
final value = prefs.get(key) ?? 0;

String myUrl = "$serverUrl/userchecks";
http.Response response = await http.get(Uri.parse(myUrl), headers: {
'Accept': 'application/json',
'Authorization': 'Bearer $value'
});

print('Response status : ${response.statusCode}');

return json.decode(response.body);
}

最佳答案

您从 JSON 返回的值不是列表,而是一个 JSON 映射,因此您必须像这样更改 Future 返回类型:

Future<Map<String,dynamic>> getData() async {
final prefs = await SharedPreferences.getInstance();
final key = 'token';
final value = prefs.get(key) ?? 0;

String myUrl = "$serverUrl/userchecks";
http.Response response = await http.get(Uri.parse(myUrl), headers: {
'Accept': 'application/json',
'Authorization': 'Bearer $value'
});

print('Response status : ${response.statusCode}');

return json.decode(response.body);
}
或者您可以按照答案进行操作 here .

关于json - Flutter 错误 : Expected a value of type 'FutureOr<List<dynamic>>' , 但得到了 '_JsonMap' 类型之一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69093363/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com