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

新学 Flutter,请教个问题

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

    腾讯云最新优惠活动来了:云产品限时1折,云服务器低至88元/年 ,点击这里立即抢购:9i0i.cn/qcloud,更有2860元代金券免费领取,付款直接抵现金用,点击这里立即领取:9i0i.cn/qcloudquan

    (福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun

    想写一个功能,对服务器进行每 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   ·   实用小工具   ·   6117 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 02:38 · PVG 10:38 · LAX 19:38 · JFK 22:38
    Developed with CodeLauncher
    ? Do have faith in what you're doing.


    http://www.vxiaotou.com