简体   繁体   English

Flutter 连接错误回调

[英]Flutter connectivity false callback

Im using connectivity package to track users connection changes.我使用连接 package 来跟踪用户连接更改。 The idea is to pop up a warning page for connection loss when the ConnectivityResult is none (aka wifi and mobile is disconnected).这个想法是在 ConnectivityResult 为无(又名 wifi 和移动设备已断开连接)时弹出连接丢失的警告页面。 But instead i get these results: If the wifi is connected and you disconnect it, 50% of the time the warning pops up.但相反,我得到了这些结果:如果 wifi 已连接并且您断开它,则 50% 的时间会弹出警告。 If you are on mobile and turn it off, the connectivity returns that user is still on ConnectivityResult.mobile not ConnectivityResult.none.如果您在移动设备上并将其关闭,则连接返回该用户仍在 ConnectivityResult.mobile 而不是 ConnectivityResult.none。

Tried to make a doublecheck with pinging google, but even that doesnt work as smooth as i expected it to be.试图通过 ping google 进行双重检查,但即使这样也没有我预期的那么顺利。

My code: I have created seperated file with functions:我的代码:我创建了具有功能的单独文件:

void trackNetworkStatus(BuildContext ctx) {
  //check in case state is just opened
  pingGoogle(ctx);

  //add network listener
  Connectivity().onConnectivityChanged.listen((ConnectivityResult result) {
    print("Network changed to $result");
    //if user lost connection
    if (result == ConnectivityResult.none) {
      openNoInternetScreen(ctx);
    }else{
      //if user has connection, doublecheck
      //mobile network is tricky on android
      pingGoogle(ctx);
    }
  });
}

Future<void> pingGoogle(BuildContext ctx) async {
  try {
    //ping internet page
    final result = await InternetAddress.lookup('google.com');

    //if ping is successful
    if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
      print('connected');
    }else{
      print('not connected');
      openNoInternetScreen(ctx);
    }
  } on SocketException catch (_) {
    print('not connected');
    openNoInternetScreen(ctx);
  }
}

void openNoInternetScreen(BuildContext ctx) {
  Navigator.push(
    ctx,
    MaterialPageRoute(builder: (context) => noInternetPage()),
  );
}

because i am calling them on every apps init like this:因为我在每个应用程序初始化时都像这样调用它们:

@override
  void initState() {
    super.initState();

    //listen to netwoek changes
    trackNetworkStatus(context);
  }

which leads to problem that sometimes warning page pops up twice because, as i believe, the previous listener has not been stopped, but i can figure out how to fix it.这会导致有时警告页面会弹出两次的问题,因为我相信之前的侦听器尚未停止,但我可以弄清楚如何解决它。 The question is why connectivity package returns false callback on mobile.问题是为什么连接 package 在移动设备上返回错误回调。 Ive tested on virtual Android API 23 and Samsung S9+, both share same results.我在虚拟 Android API 23 和三星 S9+ 上进行了测试,两者的结果相同。

I gave my app to couple early testers and everything turned out just fine for them, even if some of them own Samsung devices too.我把我的应用程序给了几个早期的测试人员,结果对他们来说一切都很好,即使他们中的一些人也拥有三星设备。 Looks like there is no problem at all with the connectivity package, some of Samsung phones just act weird as they shouldnt.看起来连接 package 完全没有问题,一些三星手机只是表现得奇怪,因为它们不应该。 Looks like there will always be a black sheep in the whole community, sadly thats me and im the developer with this buggy device.看起来整个社区中总会有一只害群之马,可悲的是我和我是这个有问题的设备的开发人员。

Havent found the fix for my device, but looks like the package is safe to go for the most of the devices in market. Havent 找到了我的设备的修复程序,但对于市场上的大多数设备,package 对 go 来说似乎是安全的。

The phone model, that is acting strange: SM-G965F.电话 model,表现得很奇怪:SM-G965F。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM