connection_info 0.0.2-dev
connection_info: ^0.0.2-dev copied to clipboard
Project to check for internet connectivity with dart.
connection_info #
Get current internet conenction information from within the Flutter application.
Platform Support #
| Android | iOS | MacOS | Web | Linux | Windows |
|---|---|---|---|---|---|
| ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
Usage #
Import package:connection_info/connection_info.dart, instantiate ConnectionController
and listen to the stream for internet connection status or check directly using the isConnected() async method.
Note: ConnectionState will conflict with package dart:async, so we import connection state as follows:
import 'package:connection_info/models/connection_state.dart'
as connection_state;
Example:
class NetworkConnectionTestScreen extends StatefulWidget {
const NetworkConnectionTestScreen({Key? key}) : super(key: key);
@override
State<NetworkConnectionTestScreen> createState() =>
_NetworkConnectionTestScreenState();
}
class _NetworkConnectionTestScreenState
extends State<NetworkConnectionTestScreen> {
StreamSubscription? connectionSubscription;
bool _isConnected = true;
@override
void initState() {
super.initState();
ConnectionController _controller = ConnectionController(
serverUrl: 'pub.dev',
repeatInterval: const Duration(seconds: 1),
showErrorInDebugMode: false,
);
connectionSubscription = _controller.stream.listen((event) {
print(event.isConnected);
if (event is connection_state.ConnectionState) {
setState(() {
_isConnected = event.isConnected;
});
}
});
}
@override
void dispose() {
super.dispose();
connectionSubscription?.cancel();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(child: Text('Has Internet: $_isConnected')),
);
}
}