【问题标题】:How to keep checking all the time for internet connection in flutter如何在颤动中一直检查互联网连接
【发布时间】:2025-12-26 01:50:16
【问题描述】:

我有一个依赖互联网工作的安卓应用程序。我需要找到一种方法来一直检查互联网是否存在以查看应用程序。否则,当没有互联网时,我应该查看一个显示没有互联网连接的小部件。

我已经尝试过 connectivity 包,但它没有按预期工作,因为它只检查开关(wifi - 移动数据)是否打开或关闭,而不是真正的互联网。

我认为向网站发送获取请求并分析响应状态代码对我来说是个好主意。所以这是我尝试过的:

检查连接类

class CheckConnectionService {
  Future<bool> checkConnection() async {
    int theCode = 0;
    Client client = Client();
    try {
      await client.get('http://www.google.com').then((value) {
        theCode = value.statusCode;
      });
    }catch(e){
      print(e);
      return false;
    }finally{
      client.close();
    }
    if (theCode == 200){
      return true;
    }
    return false;
  }
}

然后在我的 ma​​in.dart 中,我尝试使用此检查一直运行,这是我的代码:


class MyApp extends StatefulWidget {
  // This widget is the root of your application.
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  bool internetExists = true;

  Stream checkIfOnline() async* {
    await CheckConnectionService().checkConnection().then((check) {
      print(check);
    if (check){
      internetExists = true;
      setState(() {});
    } else {
      internetExists = false;
      setState(() {});
    }
    });
  }
  @override
  Widget build(BuildContext context) {
    checkIfOnline();
    if (!internetExists){
        return MaterialApp(
          title: 'G-Attend',
          home: NoInternet(),
        );
    } else{
      return MaterialApp(
        theme: ThemeData(
          fontFamily: 'Anton',
          primaryColor: Hexcolor('#691b99'),
          primaryColorLight: Hexcolor('#9b4dcb'),
          primaryColorDark: Hexcolor('#37006a'),
          secondaryHeaderColor: Hexcolor('#60C0D5'),
          accentColor: Colors.deepOrange,
        ),
        initialRoute: '/splash',
        routes: {
          '/splash': (context) => SplashScreen(),
          '/login': (context) => Login(),
          '/index': (context) => Index(),
          '/settings': (context) => Settings(),
          '/requests': (context) => Requests(),
          '/add_request': (context) => AddRequest(),
          '/request_comments': (context) => RequestComments(),
        },
      );
    }
  }
}

即使没有真正的互联网连接,它总是显示互联网应该工作的部分会发生什么。

【问题讨论】:

    标签: android ios flutter dart get


    【解决方案1】:

    要检查互联网连接,您可以使用包data_connection_checker

    bool hasConnection = false;
    
    @override
    void initState() {
      super.initState();
    
      DataConnectionChecker().onStatusChange.listen((status) {
        setState(() {
          switch(status){
            case DataConnectionStatus.connected:
              hasConnection = true;
              break;
            case DataConnectionStatus.disconnected:
              hasConnection = false;
              break;
          }
        });
      });
    }
    

    我一般去External Libraries>data_connection_checker-version

    在 dart 文件中,我将 DEFAULT_INTERNAL 更改为 1,以便每 1 秒进行一次验证:

    static const Duration DEFAULT_INTERVAL = const Duration(seconds: 1);
    

    【讨论】:

      【解决方案2】:

      您可以使用下面的插件来检查和监控连接

      https://github.com/flutter/plugins/tree/master/packages/connectivity

      import 'package:connectivity/connectivity.dart';
      
      @override
      initState() {
        super.initState();
      
        subscription = Connectivity().onConnectivityChanged.listen((ConnectivityResult result) {
          // Got a new connectivity status!
        })
      }
      
      // Be sure to cancel subscription after you are done
      @override
      dispose() {
        super.dispose();
      
        subscription.cancel();
      }
      

      【讨论】:

      • 正如问题中提到的那样,这不是一个好的解决方案,因为它并没有真正检查真正的互联网..他们也在官方文档中说:Note that on Android, this does not guarantee connection to the Internet. For instance, the app might have wifi access but it might be a VPN or a hotel WiFi with no access.
      【解决方案3】:

      这是新版本连接插件的主要示例

      // ignore_for_file: public_member_api_docs
      
      import 'dart:async';
      import 'dart:io';
      
      import 'package:connectivity/connectivity.dart';
      import 'package:flutter/foundation.dart';
      import 'package:flutter/material.dart';
      import 'package:flutter/services.dart';
      
      // Sets a platform override for desktop to avoid exceptions. See
      // https://flutter.dev/desktop#target-platform-override for more info.
      void _enablePlatformOverrideForDesktop() {
        if (!kIsWeb && (Platform.isWindows || Platform.isLinux)) {
          debugDefaultTargetPlatformOverride = TargetPlatform.fuchsia;
        }
      }
      
      void main() {
        _enablePlatformOverrideForDesktop();
        runApp(MyApp());
      }
      
      class MyApp extends StatelessWidget {
        // This widget is the root of your application.
        @override
        Widget build(BuildContext context) {
          return MaterialApp(
            title: 'Flutter Demo',
            theme: ThemeData(
              primarySwatch: Colors.blue,
            ),
            home: MyHomePage(title: 'Flutter Demo Home Page'),
          );
        }
      }
      
      class MyHomePage extends StatefulWidget {
        MyHomePage({Key key, this.title}) : super(key: key);
      
        final String title;
      
        @override
        _MyHomePageState createState() => _MyHomePageState();
      }
      
      class _MyHomePageState extends State<MyHomePage> {
        String _connectionStatus = 'Unknown';
        final Connectivity _connectivity = Connectivity();
        StreamSubscription<ConnectivityResult> _connectivitySubscription;
      
        @override
        void initState() {
          super.initState();
          initConnectivity();
          _connectivitySubscription =
              _connectivity.onConnectivityChanged.listen(_updateConnectionStatus);
        }
      
        @override
        void dispose() {
          _connectivitySubscription.cancel();
          super.dispose();
        }
      
        // Platform messages are asynchronous, so we initialize in an async method.
        Future<void> initConnectivity() async {
          ConnectivityResult result;
          // Platform messages may fail, so we use a try/catch PlatformException.
          try {
            result = await _connectivity.checkConnectivity();
          } on PlatformException catch (e) {
            print(e.toString());
          }
      
          // If the widget was removed from the tree while the asynchronous platform
          // message was in flight, we want to discard the reply rather than calling
          // setState to update our non-existent appearance.
          if (!mounted) {
            return Future.value(null);
          }
      
          return _updateConnectionStatus(result);
        }
      
        @override
        Widget build(BuildContext context) {
          return Scaffold(
            appBar: AppBar(
              title: const Text('Connectivity example app'),
            ),
            body: Center(child: Text('Connection Status: $_connectionStatus')),
          );
        }
      
        Future<void> _updateConnectionStatus(ConnectivityResult result) async {
          switch (result) {
            case ConnectivityResult.wifi:
            case ConnectivityResult.mobile:
            case ConnectivityResult.none:
              setState(() => _connectionStatus = result.toString());
              break;
            default:
              setState(() => _connectionStatus = 'Failed to get connectivity.');
              break;
          }
        }
      }
      

      【讨论】:

        【解决方案4】:

        这是因为浏览器缓存。我发现调用 cloudflare 服务器会产生一致的结果。

        【讨论】:

          最近更新 更多