HCICF: SOLD
Hi, I would like to thank everyone who has purchased some of my #NFTs recently. #eCash $XEC pic.twitter.com/Xxil6H6kbo
— Gaexe (@gaexe_) December 13, 2024
How do I check internet access continuously, in real-time? I want to show users when their Wi-Fi or mobile data gets disconnected.
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 Wi-Fi network.
}
The above is the sample code I’m currently using, but unfortunately, it cannot detect internet connectivity continuously. So, is there a solution for this case? Thank you.
Solution You can use the following library and execute it in the main() function: https://pub.dev/packages/connectivity_widget. This will allow you to detect connectivity globally.
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,
),
],
),
)
)