The error indicates that the _updateConnectionStatus
method is being treated as if it returns a List<ConnectivityResult>
, which is not correct. The _updateConnectionStatus
method returns a ConnectivityResul
indicating the current connectivity status.
There seems to be a misunderstanding in the method signature for _updateConnectionStatus
.
late StreamSubscription subscription;
StreamSubscription<ConnectivityResult>? connectivitySubscription;
StreamSubscription? internetSubscription;
bool hasInternet = false;
@override
void initState(){
super.initState();
subscription = Connectivity().onConnectivityChanged.listen(_updateConnectionStatus);
internetSubscription = InternetConnectionChecker().onStatusChange.listen((status) {
final hasInternet = status == InternetConnectionStatus.connected;
setState(() => this.hasInternet = hasInternet);
});
}
@override
void dispose() {
// Cancel the connectivity subscription when the controller is closed
connectivitySubscription?.cancel();
super.dispose();
}
void _updateConnectionStatus (ConnectivityResult result) {
final hasInternet = result != ConnectivityResult.none;
final message = hasInternet
? result == ConnectivityResult.mobile
? 'You are connected to mopile network'
: 'You are connected to mopile network'
: 'You have no internet';
// Only check for the none case, which indicates no connectivity
if (!hasInternet) {
Get.rawSnackbar(
messageText: Text('PLEASE CONNECT TO THE INTERNET $message',
style: const TextStyle(color: Colors.white, fontSize: 14)),
isDismissible: false,
duration: const Duration(days: 1),
backgroundColor: Colors.red[400]!,
icon: const Icon(
Icons.wifi_off,
color: Colors.white,
size: 35,
),
snackStyle: SnackStyle.GROUNDED);
} else {
if (Get.isSnackbarOpen) {
Get.closeCurrentSnackbar();
}
}
}
The error message is correct. You're passing your _updateConnectionStatus
method, which takes a ConnectivityResult
as the parameter, to the listen
callback of onConnectivityChanged
, which is a stream of List<ConnectivityResult>
.
On the previous versions of the connectivity_plus package, the onConnectivityChanged
stream emits a ConnectivityResult
, in which your method could be passed to it. However, the update 6.0.x introduced some breaking changes, one of them is that the onConnectivityChanged
stream now emits List<ConnectivityResult>
instead.
Your option is to either downgrade the connectivity_plus package to 5.x.x, or adapt your _updateConnectionStatus
method to accept a List<ConnectivityResult>
as the parameter.