A lightweight Flutter package for real-time internet connectivity monitoring. NetWatchX provides an easy way to check internet connection status and listen for network changes in your Flutter applications.
- π Real-time monitoring - Listen to internet connectivity changes as they happen
- β Simple API - Easy-to-use methods for checking connection status
- π¨ Builder Widget - Use
NetWatchXBuilderfor reactive UI based on connection status - π¦ Lightweight - Minimal dependencies and optimized performance
- π Easy Integration - Get started in minutes with simple setup
Add netwatchx to your pubspec.yaml:
dependencies:
netwatchx: ^1.0.0Then run:
flutter pub getAdd the following permissions to your android/app/src/main/AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />Add the following to your ios/Runner/Info.plist:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>import 'package:flutter/material.dart';
import 'package:netwatchx/netwatchx.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String networkStatus = "Checking Internet...";
@override
void initState() {
super.initState();
// Listen to network changes
NetWatchX.onStatusChange.listen((status) {
setState(() {
if (status == NetworkStatus.connected) {
networkStatus = "Internet Connected β
";
} else {
networkStatus = "No Internet β";
}
});
});
// Initial internet check
checkInternet();
}
Future<void> checkInternet() async {
bool isConnected = await NetWatchX.isConnected();
setState(() {
networkStatus = isConnected ? "Internet Connected β
" : "No Internet β";
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text("NetWatchX Example")),
body: Center(
child: Text(
networkStatus,
style: const TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
),
),
),
);
}
}import 'package:flutter/material.dart';
import 'package:netwatchx/netwatchx.dart';
class MyWidget extends StatelessWidget {
const MyWidget({super.key});
@override
Widget build(BuildContext context) {
return NetWatchXBuilder(
online: const Text('You are online! β
'),
offline: const Text('You are offline! β'),
);
}
}// One-time check
bool isConnected = await NetWatchX.isConnected();
if (isConnected) {
print('Internet is available');
} else {
print('No internet connection');
}StreamSubscription<NetworkStatus> subscription =
NetWatchX.onStatusChange.listen((status) {
if (status == NetworkStatus.connected) {
print('Connected to internet');
} else {
print('Disconnected from internet');
}
});
// Don't forget to cancel the subscription when done
subscription.cancel();static Stream<NetworkStatus> onStatusChange- Stream that emits network status changesstatic Future<bool> isConnected()- Returnstrueif internet is available,falseotherwise
online(required) - Widget to display when internet is connectedoffline(required) - Widget to display when internet is disconnected
Enum with two values:
NetworkStatus.connected- Internet is availableNetworkStatus.disconnected- Internet is not available
Contributions are welcome! If you find a bug or want to add a feature:
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
If you encounter any issues or have questions, please file them on the issue tracker.
This project is licensed under the MIT License - see the LICENSE file for details.
Created by UKSolutions
If you like this package, please give it a β on GitHub!