【问题标题】:why doesn't HTTP package work in Flutter web?为什么 HTTP 包在 Flutter web 中不起作用?
【发布时间】:2020-01-19 04:09:32
【问题描述】:

我正在尝试使用 HTTP 执行获取请求,只是为了获取 google 并将其显示为文本。

我的代码适用于 Android 和 iOS,但不适用于 Web。

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),
    ),
  );
}

【问题讨论】:

    标签: flutter flutter-web


    【解决方案1】:

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

    我相信它会有所帮助:))

    【讨论】:

    • 后端工作正常,但我没有办法在不使用第三个站点的情况下获得像 google.com 这样的任何站点吗?
    猜你喜欢
    • 2019-11-05
    • 2020-11-15
    • 2022-08-02
    • 2021-05-26
    • 2021-07-24
    • 2018-12-17
    • 1970-01-01
    • 2022-10-01
    • 1970-01-01
    相关资源
    最近更新 更多