如何在flutter.dart中连续检查互联网访问,而不是连接 [英] How do you check internet ACCESS continously in flutter.dart, not connection
本文介绍了如何在flutter.dart中连续检查互联网访问,而不是连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您如何连续检查互联网访问?我能够显示wifi是否已连接或移动数据.但是,并非所有连接都可以访问互联网.
how do you continously check internet access? I was able to show if wifi is connectected or mobile data. However, not all connection would have internet access.
import 'package:connectivity/connectivity.dart';
var connectivityResult = await (Connectivity().checkConnectivity());
if (connectivityResult == ConnectivityResult.mobile) {
// I am connected to a mobile network.
} else if (connectivityResult == ConnectivityResult.wifi) {
// I am connected to a wifi network.
}
这是当前正在使用的代码. 我希望有人可以不断检查互联网访问吗?
This is currently the code im using. I was hoping how someone could check continously of internet access?
推荐答案
您可以使用此 https://pub .dev/packages/connectivity_widget .安装此软件包后,请在您的构建函数中使用以下代码.
You can use this https://pub.dev/packages/connectivity_widget . By installing this package use this code in your build function.
ConnectivityWidget(
builder: (context, isOnline) => Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("${isOnline ? 'Online' : 'Offline'}", style: TextStyle(fontSize: 30, color: isOnline ? Colors.green : Colors.red),),
SizedBox(height: 20,),
Text(
'Number of times we connected to the internet:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),
],
),
)
这篇关于如何在flutter.dart中连续检查互联网访问,而不是连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文