新学 Flutter,请教个问题

2022-03-07 17:23:59 +08:00
 chengxiao

想写一个功能,对服务器进行每 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
1979 次点击
所在节点    Flutter
1 条回复
serious198706
2022-03-31 17:13:34 +08:00
之前的 timer 没 cancel 掉。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/838656

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX