想写一个功能,对服务器进行每 5 秒 1 次请求,如果达到最大尝试次数,就跳转到屏保页面,现在遇到个问题,就是在我更改了全局变量后,终端依旧会对之前的全局配置进行错误请求。
@override
void initState() {
super.initState();
setState(() {
if (Global.serverAddress == "") {
Global.serverAddress = "127.0.0.1";
Global.serverPort = "80";
}
this.host = Global.serverAddress + ":" + Global.serverPort;
print("++++++++++++++++++++++++++++++++++++++++++++");
print(this.host);
});
timer = Timer.periodic(Duration(seconds: 5), (Timer t) => get());
}
get() async {
var full = "http://" + this.host + "/data.json";
var url = Uri.parse(full);
var response = await http.get(url);
Map data = json.decode(response.body);
setState(() {
wrongTry++;
setComputeState(data);
});
}
当我从其他页面把配置改成"192.168.100.156"后,虽然画面渲染是 OK 的,但是后台还是不停的对之前的错误地址 127.0.0.1 进行重试,这是什么原因呢?
I/flutter ( 5764): ++++++++++++++++++++++++++++++++++++++++++++
I/flutter ( 5764): 192.168.100.156:8085
E/flutter ( 5764): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: SocketException: OS Error: Connection refused, errno = 111, address = 127.0.0.1, port = 51088
E/flutter ( 5764):
E/flutter ( 5764): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: SocketException: OS Error: Connection refused, errno = 111, address = 127.0.0.1, port = 51092
E/flutter ( 5764):
E/flutter ( 5764): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: SocketException: OS Error: Connection refused, errno = 111, address = 127.0.0.1, port = 51096
E/flutter ( 5764):
E/flutter ( 5764): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: SocketException: OS Error: Connection refused, errno = 111, address = 127.0.0.1, port = 51100
1
serious198706 2022-03-31 17:13:34 +08:00
之前的 timer 没 cancel 掉。
|