gpt4 book ai didi

flutter - Flutter:WAITINGHTTP请求进入循环

转载 作者:行者123 更新时间:2023-12-03 04:50:17 25 4
gpt4 key购买 nike

我在执行http异步请求时遇到了奇怪的问题。我知道它失败的原因,但不知道如何解决。
这是我的代码。

    Future userLogin() async{
try{
String email = emailController.text;
String password = passwordController.text;
var url = 'http://192.168.100.4:8080/myapp/loginnexec.php';

// Store all data with Param Name.
var data = {'email': email, 'password' : password };
print(data);
// Starting Web API Call.
var response = await http.post(url, body: json.encode(data));

// Getting Server response into variable.

final message = loginFromJson(response.body);
if(response.statusCode == 200){
setState(() {
visible = false;
});

}

}
}catch(e){
.....
}
}

如果我的Web服务器已关闭或文件不可用,则它将进入无限循环。我的意思是,我一直都能看到相同的JSON数据(var data = {'email':email,'password':password}。

我想知道如何防止它循环。看起来它正在尝试访问Web服务器以获取数据,如果未获取数据,则再次尝试。

我不确定我是否正确解释了。

对不起,我的英语不好。

最佳答案

您可以在timeout上设置Future,以便在一定时间后解析:

await http.post('someurl').timeout(Duration(seconds: 5),onTimeout: (){
throw Exception();
//or you can also
// return null;
});

关于flutter - Flutter:WAITINGHTTP请求进入循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61527740/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com