Flutter 没有网络时,如何在固定页面上暂停我的应用程序?

Flutter 没有网络时,如何在固定页面上暂停我的应用程序?,flutter,dart,Flutter,Dart,如果没有internet连接,我想停止我的应用程序工作(显示固定页面,“没有连接”),它将持续检查internet连接。您可以使用以下插件收听internet连接: 但我使用以下方法: _checkInternetConnection() { Timer(Duration(seconds: 3), () { _splashFunction(); }); } _splashFunction() async { AppUtil.checkInternetConn

如果没有internet连接,我想停止我的应用程序工作(显示固定页面,“没有连接”),它将持续检查internet连接。

您可以使用以下插件收听internet连接:

但我使用以下方法:


_checkInternetConnection() {
    Timer(Duration(seconds: 3), () {
      _splashFunction();
    });
  }

_splashFunction() async {
AppUtil.checkInternetConnectivity().then((isOnline) async {
if (isOnline) {...}else{_checkInternetConnection()}})}


static Future<bool> checkInternetConnectivity() async {
    bool isConnected;
    try {
      final result = await InternetAddress.lookup('google.com');
      if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
        isConnected = true;
      }
    } on SocketException catch (_) {
      isConnected = false;
    }
    return isConnected;
  }

_checkInternetConnection(){
计时器(持续时间(秒数:3),(){
_splash函数();
});
}
_splash函数()异步{
AppUtil.checkInternetConnectivity().then((isOnline)异步{
if(isOnline){…}else{{u checkInternetConnection()}}}
静态未来检查InternetConnectivity()异步{
布尔断开连接;
试一试{
最终结果=等待InternetAddress.lookup('google.com');
if(result.isNotEmpty&&result[0].rawAddress.isNotEmpty){
断开连接=正确;
}
}关于SocketException捕获(41;{
断开连接=错误;
}
返回不连接;
}
您可以将包与
StreamBuilder一起使用

StreamBuilder(
  stream: Connectivity().onConnectivityChanged,
  builder: (context, snapshot) {
    // Initially snapshot might be null, so use this to avoid errors
    if (snapshot.connectionState == ConnectionState.none) {
      return CircularProgressIndicator();
    } else {
      ConnectivityResult result = snapshot.data;

      // Check Connectivity result here and display your widgets
      if(ConnectivityResult.none) {
        yourWidgetForNoInternet();
      } else {
        yourWidgetForInternet();
      }
    }
  },
)

你可以使用这个软件包

截图


有一个pub.dartlang.org/packages/connectivity插件来检查网络是否已连接。您可以反复尝试访问internet中的服务器,以检查网络是否连接到internet。此外,我认为您可以路由页面并使用WillPopScope,如果不存在连接,则取消pop。