我已经为Web应用建立了自己的第一个工具。这是我的相关代码:
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;
}
}
问题
它在localhost上工作正常。但是当我部署到Firebase托管时,它陷入了困境
http.Response response2 = await http.post(getMenuDataURL, body...
AND 有时,它在以下语句中将response1作为未定义对象抛出异常:
debugPrint('Response status: ${response1.statusCode}');
不确定发生了什么,请寻求帮助。
答案 0 :(得分:0)
所以我最终使用调试控制台发现了错误。 我是从Https托管服务器发出http请求,因此出现了浏览器阻止的混合内容错误。为了进行测试,我将网站托管在http服务器上,并且工作正常!