【问题标题】:Flutter Web getting stuck at Http request when hosted - Working fine locallyFlutter Web 在托管时卡在 Http 请求上 - 在本地工作正常
【发布时间】:2021-02-23 11:47:32
【问题描述】:

我已经为网络应用构建了我的第一个 Flutter。这是我的相关代码:

  String status='Starting...';

  CPConstructor () {
    downloadMenu();
  }

  void changeStatus ( String status) {
      this.status = status;
      notifyListeners();
  }

  void downloadMenu () async {
    try {
      var getRestDetailsURL = "some Api URL";
      var getMenuDataURL = 'some Api URL';
      changeStatus("Connecting...");
      http.Response response1 = await http.post(getRestDetailsURL, body: {'res_key': resKey, 'auth_key': authKey}).timeout(Duration(seconds: 1), onTimeout: (){
        changeStatus("Connection timeout...\n Please refresh the page.");
        return;
      });
      changeStatus("Downloading latest menu..."); // Getting STUCK here
      http.Response response2 = await http.post(getMenuDataURL, body: {'res_key': resKey, 'auth_key': authKey}).timeout(Duration(seconds: 1),onTimeout: (){
        changeStatus("Please refresh the page...");
        return;
      });
       debugPrint('Response status: ${response1.statusCode}');
       // Sometimes throwing exception for undefined object for response1??????
       debugPrint('Response body: ${response1.body}', wrapWidth:1024);
       debugPrint('Response status: ${response2.statusCode}');
       debugPrint('Response body: ${response2.body}', wrapWidth:1024);
      
      dataLoaded = true;
      notifyListeners();
      trialCounter=0;
      return;
    } on TimeoutException catch(e){
      print(e);
      trialCounter++;
      if (trialCounter<3) downloadMenu();
      return;
    }
  }

问题 它在本地主机上运行良好。但是当我部署到 Firebase 托管时,它就卡在了 http.Response response2 = await http.post(getMenuDataURL, body...

有时它会在以下语句中将 response1 作为未定义对象抛出异常:

debugPrint('Response status: ${response1.statusCode}');

不确定发生了什么,需要一些帮助。

【问题讨论】:

  • 对于托管站点来说,超时时间太短了 1 秒。可能在本地运行良好,但托管时可能会导致错误。
  • 从 1 变成 5,还是一样的问题。

标签: firebase flutter flutter-layout firebase-hosting flutter-web


【解决方案1】:

所以我最终使用调试控制台发现了错误。 我是在来自 Https 主机的 http 请求中发出的,所以它是浏览器阻止的混合内容错误。为了进行测试,我将网站托管在 http 服务器上,效果很好!

【讨论】:

    猜你喜欢
    • 2020-02-21
    • 2021-11-11
    • 1970-01-01
    • 2020-08-13
    • 2021-11-14
    • 2019-03-12
    • 2017-08-28
    • 1970-01-01
    • 2016-09-24
    相关资源
    最近更新 更多