我正在尝试在flutter ajax post中做一个ajax帖子
这是我的代码。
get() async {
var url = 'https://jsonplaceholder.typicode.com/users';
var httpClient = new HttpClient();
String result;
try {
var request = await httpClient.getUrl(Uri.parse(url));
var response = await request.close();
if (response.statusCode == HttpStatus.OK) {
var jsonString = await response.transform(utf8.decoder).join();
var data = json.decode(jsonString);
result = data;
} else {
result =
'Error getting IP address:\nHttp status ${response.statusCode}';
}
} catch (exception) {
result = 'Failed getting IP address';
}
print(result);
}
当我尝试https://httpbin.org/ip
时,它运行正常。
但由于https://jsonplaceholder.typicode.com/users
它失败了,我很吵,也很惹人喜欢。
我打印出它抛出的异常。
type '_BoundSinkStream<dynamic, List<int>>' is not a subtype of type '_HttpIncoming' where _BoundSinkStream is from dart:async List is from dart:core int is from dart:core List is from dart:core int is from dart:core _HttpIncoming is from dart:_http List is from dart:core int is from dart:core
无法做出任何改变。
更改为String result to List result = []
type '_BoundSinkStream<dynamic, List<int>>' is not a subtype of type '_HttpIncoming' where
_BoundSinkStream is from dart:async
List is from dart:core
int is from dart:core
List is from dart:core
int is from dart:core
_HttpIncoming is from dart:_http
List is from dart:core
int is from dart:core
使用更多调试信息更新了代码:
getData() async {
var url = 'https://jsonplaceholder.typicode.com/users';
var httpClient = new HttpClient();
List result = [];
try {
print('0');
var request = await httpClient.getUrl(Uri.parse(url));
print('1');
var response = await request.close();
print('2');
if (response.statusCode == HttpStatus.OK) {
print('3');
var jsonString = await response.transform(utf8.decoder).join();
print('4');
var data = json.decode(jsonString);
print('5');
result = data;
print('6');
} else {
// result = 'Error getting IP address:\nHttp status ${response.statusCode}';
}
} catch (exception) {
// result = 'Failed getting IP address';
print('7');
print(exception);
print('8');
}
print(result);
}
例外:
0
1
2
3
7
type '_BoundSinkStream<dynamic, List<int>>' is not a subtype of type '_HttpIncoming' where
_BoundSinkStream is from dart:async
List is from dart:core
int is from dart:core
List is from dart:core
int is from dart:core
_HttpIncoming is from dart:_http
List is from dart:core
int is from dart:core
8
[]
颤动版
Flutter 0.1.5 • channel beta • https://github.com/flutter/flutter.git
Framework • revision 3ea4d06340 (5 weeks ago) • 2018-02-22 11:12:39 -0800
Engine • revision ead227f118
Tools • Dart 2.0.0-dev.28.0.flutter-0b4f01f759
真的很老我更新了颤动
Flutter 0.2.3 • channel beta • https://github.com/flutter/flutter.git
Framework • revision 5a58b36e36 (3 weeks ago) • 2018-03-13 13:20:13 -0700
Engine • revision e61bb9ac3a
Tools • Dart 2.0.0-dev.35.flutter-290c576264
现在我有了一个新错误。
0
1
2
3
4
5
7
type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List' where
_InternalLinkedHashMap is from dart:collection
String is from dart:core
List is from dart:core
8
我更新了GitHub帖子中的代码
getData() async {
print("0");
var response = await http.get(
Uri.encodeFull("https://jsonplaceholder.typicode.com/users"),
headers: {"Accept": "application/json"});
print("1");
data = json.decode(response.body);
print("2");
print(data[1]["name"]);
print("3");
// print(response.body);
print("end");
}
这是有效的。
答案 0 :(得分:2)
要解码JSON数组,您需要一个字符串列表,尝试更改
String result;
带
List result = [];
答案 1 :(得分:1)
我不知道你正在使用什么http包。
您可以使用此http package
并且它使用以下代码同时使用url
var url = 'https://jsonplaceholder.typicode.com/users';
//var url = "https://httpbin.org/ip";
http.get(url)
.then((response) {
print("Response status: ${response.statusCode}");
print("Response body: ${response.body}");
});