为什么 HTTP 包在 Flutter web 中不起作用?
why doesn't HTTP package work in Flutter web?
我正在尝试使用 HTTP 执行获取请求,只是为了获取 google 并将其显示为文本。
我的代码在 Android 和 iOS 中都有效,但在网络中无效。
String _google;
void _onClick() async {
final _res = await http.get('https://www.google.com');
print(_res.body);
setState(() => _google = _res.body);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(''),
),
body: Center(
child: Text(_google ?? 'null'),
),
floatingActionButton: FloatingActionButton(
onPressed: _onClick,
child: Icon(Icons.send),
),
);
}
我不知道是什么导致了这个问题的发生,但一个好的解决方法是使用网站 cors-anywhere.herokuapp.com
作为代理;
或将 res.header('Access-Control-Allow-Origin', '*')
添加到您的后端响应中。
我相信它有帮助:))
我正在尝试使用 HTTP 执行获取请求,只是为了获取 google 并将其显示为文本。
我的代码在 Android 和 iOS 中都有效,但在网络中无效。
String _google;
void _onClick() async {
final _res = await http.get('https://www.google.com');
print(_res.body);
setState(() => _google = _res.body);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(''),
),
body: Center(
child: Text(_google ?? 'null'),
),
floatingActionButton: FloatingActionButton(
onPressed: _onClick,
child: Icon(Icons.send),
),
);
}
我不知道是什么导致了这个问题的发生,但一个好的解决方法是使用网站 cors-anywhere.herokuapp.com
作为代理;
或将 res.header('Access-Control-Allow-Origin', '*')
添加到您的后端响应中。
我相信它有帮助:))