为什么Flutter网站无法使用HTTP包?

时间:2019-09-18 08:51:02

标签: flutter flutter-web

我正在尝试使用HTTP进行get请求,只是为了获取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),
    ),
  );
}

1 个答案:

答案 0 :(得分:1)

我不知道是什么导致了此问题的发生,但是一个好的解决方法是使用网站cors-anywhere.herokuapp.com作为代理; 或将res.header('Access-Control-Allow-Origin', '*')添加到您的后端响应中。

我确定这会有所帮助:))