How to Check Internet Connection in Flutter (HCICF)


HCICF:    SOLD




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,
          ),
        ],
      ),
    )
)

Post a Comment

Previous Next

نموذج الاتصال