A tiny package to monitor network status on real time. This is wrapper package around connectivity_plus.
Features
You can monitor network status at following level
- Global level
- Screen level
- Widget level
Getting started
to use this in your flutter application. intialize NetworkMonitor in main method.
await NetworkMonitor().initialize();
Usage
to use it at global level define navigatorObservers parameter inside the material app.
navigatorObservers: [
NetworkAwareNavigatorObserver(
networkStatusScreen: const NetworkStatusScreen(),
errorType: ErrorType.widget,
)
],
to use at screen level you can use the mixin class
class _NetworkMonitorWidgetState extends State<NetworkMonitorWidget>
with NetworkAwareMixin {
@override
void initState() {
super.initState();
initializeNetworkMonitoring((state) {
switch (state) {
case NetworkState.connected:
debugPrint("connection state connected");
break;
case NetworkState.disconnected:
debugPrint("connection state disconnected");
break;
case NetworkState.noInternet:
debugPrint("connection state no internet");
break;
default:
}
});
}
@override
void dispose() {
super.dispose();
disposeNetworkMonitoring();
}
To use at widget level you can use the streambuilder
StreamBuilder<NetworkState>(
stream: NetworkMonitor().state,
builder: (context, snapshot) {
// Build based on connection state
return Text('Network status check ${snapshot.data?.name}');
},
),
if you just want to ensure the connection is connected or not you can use this static method.
final bool isCOnnected = await NetworkMonitor.isConnected();
Additional information
Feel free to enhance the package by raising the PR and opening the issue.