connection_info 0.0.2-rc copy "connection_info: ^0.0.2-rc" to clipboard
connection_info: ^0.0.2-rc copied to clipboard

outdated

Project to check for internet connectivity with dart.

connection_info #

pub package

Get current internet conenction information from within the Flutter application.

Platform Support #

Android iOS MacOS Web Linux Windows
✔️ ✔️ ✔️ ✔️ ✔️ ✔️

Sponshorship #

If you like our work, please consider supporting us for coffees and future developments. You can sponsor us on ![alt text](https://img.buymeacoffee.com/button-api/?text=Buy me a coffee&emoji=&slug=ExpertKiD&button_colour=FFDD00&font_colour=000000&font_family=Cookie&outline_colour=000000&coffee_colour=ffffff)

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')),
    );
  }
}