V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
chengxiao
V2EX  ›  Flutter

新学 Flutter,请教个问题

  •  
  •   chengxiao · 2022-03-07 17:23:59 +08:00 · 1979 次点击
    这是一个创建于 994 天前的主题,其中的信息可能已经有所发展或是发生改变。

    想写一个功能,对服务器进行每 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 条回复    2022-03-31 17:13:34 +08:00
    serious198706
        1
    serious198706  
       2022-03-31 17:13:34 +08:00
    之前的 timer 没 cancel 掉。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3046 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 14:13 · PVG 22:13 · LAX 06:13 · JFK 09:13
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.