I have started development in flutter. In one scenario I have to check if the user is connected to internet or not. I am using connectivity 0.4.9+5 and data_connection_checker 0.3.4 in combination to check if user has active connection or not.
I have create one file internet_connectivity_service.dart which perform all the task related to it.
class InternetConnectivityService {
StreamSubscription<ConnectivityResult> _subscription;
/*
* This function check if the device has internet connectivity.
* With the help of DataConnectionChecker it checks for actual internet connectivity.
*/
Future<bool> _checkConnectivity() async {
if (await DataConnectionChecker().hasConnection) {
return true;
} else {
return false;
}
}
/*
* This function is invoked to on each connection change request.
* Using Connectivity package it will check for connectivity and connection type.
* This function has return type of Future<bool> which perform async request to check connectivity.
* After checking connection type it check for divice has internet accesssible or not.
*/
Future<bool> _hasInternet() async {
var connectivityResult = await (Connectivity().checkConnectivity());
if (connectivityResult == ConnectivityResult.mobile) {
return _checkConnectivity();
} else if (connectivityResult == ConnectivityResult.wifi) {
return _checkConnectivity();
} else {
return false;
}
}
StreamSubscription<ConnectivityResult> initInterNetConnectivitySubscription(
BuildContext context) {
// * Subscribe to connectivity changes
_subscription = Connectivity().onConnectivityChanged.listen(
(ConnectivityResult result) {
_hasInternet().then(
(result) {
// * When internet is disconnected
Size size = MediaQuery.of(context).size;
if (!result) {
Scaffold.of(context).showSnackBar(
snackBarBuilder(
color: kErrorColor,
text: 'Connection Lost',
context: context),
);
} else {
Scaffold.of(context).showSnackBar(
snackBarBuilder(
color: kSuccessColor, text: 'Connected', context: context),
);
}
},
);
},
);
return _subscription;
}
disposeSubscription() {
_subscription.cancel();
}
}
Function initInterNetConnectivitySubscription
and disposeSubscription
are used for subscribe and unsubscribe for particular event. I am calling these methods from component where I want this functionality.
For Example:
InternetConnectivityService internetConnectivityService =
InternetConnectivityService();
@override
initState() {
internetConnectivityService.initInterNetConnectivitySubscription(context);
super.initState();
}
@override
dispose() {
internetConnectivityService.disposeSubscription();
super.dispose();
}
By doing so functionality is working sometimes but sometimes I am facing error stating E/EventChannel#plugins.flutter.io/connectivity_status(28677): Failed to close existing event stream
.
Complete stack trace.
Restarted application in 5,259ms.
E/EventChannel#plugins.flutter.io/connectivity_status(28677): Failed to close existing event stream
E/EventChannel#plugins.flutter.io/connectivity_status(28677): java.lang.IllegalArgumentException: NetworkCallback was not registered
E/EventChannel#plugins.flutter.io/connectivity_status(28677): at com.android.internal.util.Preconditions.checkArgument(Preconditions.java:50)
E/EventChannel#plugins.flutter.io/connectivity_status(28677): at android.net.ConnectivityManager.unregisterNetworkCallback(ConnectivityManager.java:4009)
E/EventChannel#plugins.flutter.io/connectivity_status(28677): at io.flutter.plugins.connectivity.ConnectivityBroadcastReceiver.onCancel(ConnectivityBroadcastReceiver.java:53)
E/EventChannel#plugins.flutter.io/connectivity_status(28677): at io.flutter.plugin.common.EventChannel$IncomingStreamRequestHandler.onListen(EventChannel.java:182)
E/EventChannel#plugins.flutter.io/connectivity_status(28677): at io.flutter.plugin.common.EventChannel$IncomingStreamRequestHandler.onMessage(EventChannel.java:167)
E/EventChannel#plugins.flutter.io/connectivity_status(28677): at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:85)
E/EventChannel#plugins.flutter.io/connectivity_status(28677): at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:692)
E/EventChannel#plugins.flutter.io/connectivity_status(28677): at android.os.MessageQueue.nativePollOnce(Native Method)
E/EventChannel#plugins.flutter.io/connectivity_status(28677): at android.os.MessageQueue.next(MessageQueue.java:336)
E/EventChannel#plugins.flutter.io/connectivity_status(28677): at android.os.Looper.loop(Looper.java:174)
E/EventChannel#plugins.flutter.io/connectivity_status(28677): at android.app.ActivityThread.main(ActivityThread.java:7356)
E/EventChannel#plugins.flutter.io/connectivity_status(28677): at java.lang.reflect.Method.invoke(Native Method)
E/EventChannel#plugins.flutter.io/connectivity_status(28677): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
E/EventChannel#plugins.flutter.io/connectivity_status(28677): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)**strong text**
Due to this error my application is not getting crash but sometime functionality doesnot work as desired. Please let me know if I am making some mistake or if their is better way of doing so. I just wan't to have functionality in which when user don't have connection than application will show stating that you are not connected to internet.
I assume you have already solved this problem so this if for the people who come in the future and still have this problem.
I fixed it by upgrading to the latest version where they fixed the issue:
connectivity: ^2.0.2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With