如何让 ajax 陷入混乱?
How to do an ajax get in flutter?
我正在尝试 ajax post in flutter ajax post
这是我的代码。
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 版本
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
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 post
更新了代码
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");
}
这是有效的。
要解码 JSON 数组,您需要一个字符串列表,尝试更改
String result;
和
List result = [];
不知道你用的是什么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}");
});
我正在尝试 ajax post in flutter ajax post
这是我的代码。
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 版本
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
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 post
更新了代码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");
}
这是有效的。
要解码 JSON 数组,您需要一个字符串列表,尝试更改
String result;
和
List result = [];
不知道你用的是什么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}");
});