开发工具中显示的错误消息:
我正在尝试在 Flutter 中执行 PHP 文件。
我的代码是:
class _MyAppState extends State<MyApp> {
@override
var url = 'http://localhost/myDashFolder/getdata.php';
Future<List> getData() async {
final response = await http.get(Uri.parse(url));
if (response.statusCode == 200) {
return json.decode(response.body);
}
else {
throw Exception("Response content length is ${response.statusCode}, failed to get any details.");
}
}
在开发工具中,获取请求显示错误。但是,当我手动执行相同的操作时,它会起作用。
我做错了什么?
在我的例子中,http.response 和 http.get 不起作用。
这是对我有用的代码。
void getData() async {
var response= await get(Uri.parse("your URL with http:// or https://"));
if (response.statusCode == 200) {
var jsonResponse =
convert.jsonDecode(response.body) as Map<String, dynamic>;
var itemCount = jsonResponse['totalItems'];
print('Count: $itemCount.');
} else {
print('Request failed with status: ${response.statusCode}.');
}
}