[英]Check or listen continuously to internet connection/Network Connectivity in dart, flutter app
I have been searching for long to know the best approach to listen to internet connection in flutter/dart app.我一直在寻找很久才能知道在颤振/飞镖应用程序中收听互联网连接的最佳方法。 I think this approach is better for now and it can be of help to some like me who has been searching.我认为这种方法现在更好,它可以帮助像我这样一直在寻找的人。 I have used many connectivity plugins, but it didn't work.我使用了许多连接插件,但都不起作用。 I have equally used data_connection_checker, lookUpAddress etc as suggested by many but to no avail.我同样使用了许多人建议的 data_connection_checker、lookUpAddress 等,但无济于事。 But below helped.但下面有帮助。 Use the below plugins to check or listen to Internet Connection / Network Connectivity in dart, flutter app.使用以下插件检查或收听 dart、flutter 应用程序中的 Internet 连接/网络连接。
internet_connection_checker internet_connection_checker
import 'dart:async';
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:internet_connection_checker/internet_connection_checker.dart';
class ConnectionUtil {
static final ConnectionUtil _singleton = new ConnectionUtil._internal();
ConnectionUtil._internal();
static ConnectionUtil getInstance() => _singleton;
bool hasConnection = false;
StreamController connectionChangeController = StreamController();
final Connectivity _connectivity = Connectivity();
void initialize() {
_connectivity.onConnectivityChanged.listen(_connectionChange);
}
void _connectionChange(ConnectivityResult result) {
_hasInternetInternetConnection();
}
Stream get connectionChange => connectionChangeController.stream;
Future<bool> _hasInternetInternetConnection() async {
bool previousConnection = hasConnection;
var connectivityResult = await (Connectivity().checkConnectivity());
if (connectivityResult == ConnectivityResult.mobile || connectivityResult == ConnectivityResult.wifi) {
// this is the different
if (await InternetConnectionChecker().hasConnection) {
hasConnection = true;
} else {
hasConnection = false;
}
} else {
hasConnection = false;
}
if (previousConnection != hasConnection) {
connectionChangeController.add(hasConnection);
}
return hasConnection;
}
}
Implement this code on the stateful widget.....在有状态小部件上实现此代码.....
bool hasInterNetConnection = false;
@override
initState() {
ConnectionUtil connectionStatus = ConnectionUtil.getInstance();
connectionStatus.initialize();
connectionStatus.connectionChange.listen(connectionChanged);
super.initState();
}
void connectionChanged(dynamic hasConnection) {
setState(() {
hasInterNetConnection = hasConnection;
});
}
Good luck祝你好运
I had faced a similar problem a few weeks ago.几周前我也遇到过类似的问题。 This is a good approach.这是一个很好的方法。 The internet_connection_checker
plugin allows one to address issues at the network layer that the connectivity_plus
plugin cannot address. internet_connection_checker
插件允许解决connectivity_plus
插件无法解决的网络层问题。 I have carried out an implementation of these two plugins using the bloc
library.我已经使用bloc
库执行了这两个插件的实现。 For more information and code refer to this Stackoverflow post and this Github issue.有关更多信息和代码,请参阅此Stackoverflow 帖子和此Github 问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.