You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter使用connectivity_plus检测网络连接时的异常问题排查

问题分析

你遇到的矛盾现象(connectivityResult显示为ConnectivityResult.none,但checkInternetConnectivity()仍返回true),大概率是connectivity包的局限性或枚举值匹配问题导致的:

  • connectivity包仅能检测设备是否连接到本地网络(如WiFi、移动数据),无法验证网络是否能真正访问互联网;
  • 模拟器可能存在网络状态缓存,或枚举值的字符串显示与实际枚举成员存在隐性不匹配,导致判断逻辑失效。

解决方案

1. 先排查枚举值匹配问题

打印枚举值的具体信息,确认checkConnectivity()返回的结果与ConnectivityResult.none是否真的相等:

Future<bool> checkInternetConnectivity() async {
  try {
    final connectivityResult = await Connectivity().checkConnectivity();
    print('实际返回值: $connectivityResult'); 
    print('预期None值: ${ConnectivityResult.none}');
    print('是否相等: ${connectivityResult == ConnectivityResult.none}');
    return connectivityResult != ConnectivityResult.none;
  } catch (e) {
    print('检测错误: $e'); 
    return false;
  }
}

如果打印显示两者不相等但字符串类似,尝试重新安装connectivity依赖包,或确认导入的包版本与枚举定义一致。

2. 改用「本地检测+互联网验证」的可靠方案

为彻底避免connectivity包的局限性,添加真实网络访问验证(比如DNS解析):

Future<bool> checkInternetConnectivity() async {
  try {
    // 第一步:检测本地网络连接状态
    final connectivityResult = await Connectivity().checkConnectivity();
    if (connectivityResult == ConnectivityResult.none) {
      print('无本地网络连接');
      return false;
    }
    // 第二步:验证是否能访问互联网
    final response = await InternetAddress.lookup('google.com')
        .timeout(const Duration(seconds: 3));
    return response.isNotEmpty && response[0].rawAddress.isNotEmpty;
  } on SocketException catch (_) {
    print('无互联网访问');
    return false;
  } on TimeoutException catch (_) {
    print('网络请求超时');
    return false;
  } catch (e) {
    print('检测错误: $e');
    return false;
  }
}

这种组合方式既保证了本地网络检测的效率,又能验证真实的互联网连通性,比单纯依赖connectivity包更准确。

3. 优化Timer中的异步逻辑

去掉冗余的await firebaseAuth.currentUser(currentUser本身不是Future),并添加日志确认流程:

void startTimer() {
  Timer(const Duration(seconds: 5), () async {
    print('开始检测网络...');
    bool isConnected = await checkInternetConnectivity();
    print('最终网络状态: $isConnected');

    if (isConnected) {
      if (firebaseAuth.currentUser != null) {
        AssistanceMethods.readCurrentUserOnlineInfo();
        Navigator.pushReplacement(context, MaterialPageRoute(builder: (c) => const MainScreen()));
      } else {
        Navigator.pushReplacement(context, MaterialPageRoute(builder: (c) => const LoginScreen()));
      }
    } else {
      Navigator.pushReplacement(context, MaterialPageRoute(builder: (c) => const ErrorMsgScreen()));
    }
  });
}

内容的提问来源于stack exchange,提问作者Godwin Addetsi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 11:30:12