Connectivity
Use Connectivity package to discover network connectivity types that can be used.Implementing Connectivity:
Connectivity registered as singleton with Injectable, so you need to call `configureDependencies` in your app main.dart before initialize listening for connectivity changes:void main() {
// ...
// config all dependencies including "AppConnectivity"
await configureAppDependencies();
// listen for connection changes
GetIt.instance<AppConnectivity>().initialize();
runApp(const MyApp());
}
Check Connection:
// check if device connected to network
final LiveData<bool> isConnected = GetIt.instance<AppConnectivity>().isConnected;
final bool connected = isConnected.value;
// render UI depending on connectivity status
LiveDataBuilder<bool>(
data: GetIt.instance<AppConnectivity>().isConnected,
builder: (BuildContext context, bool isConnected) {
if (!isConnected) {
return Text("check your connection", style: TextStyle(color: Colors.red));
} else {
return Text("connected", style: TextStyle(color: Colors.green));
}
}
)