connection_info 1.0.0 copy "connection_info: ^1.0.0" to clipboard
connection_info: ^1.0.0 copied to clipboard

The library helps to check for internet conenctivity in all platforms supported by Flutter. It provides a stream to continuously listen for internet conenction state and a future to check for internet [...]

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 by clicking the below link to proceed.

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;

1. ConnectionController #

The ConnectionController class has one constructor. By default, the serverUrl is set to google.com, repeatInterval to check for internet connection is set to 1 second and showErrorInDebugMode is set to false.

Syntax:

ConnectionController(
    {String serverUrl = 'google.com',
    Duration repeatInterval = const Duration(seconds: 1),
    bool showErrorInDebugMode = false,
})

The ConnectionController has one public stream named stream that will return a ConnectionState object both on data and error state. Similarly, we can use Future<ConnectionState> isConnected() async method of ConnectionController class to check the current status of the internet.

The idea here is, we only want to know whether there is internet or not. Not, how internet is provided to us. So, we only check if there is internet or not at any given time periodically using repeatInterval.

2. ConnectionState #

The ConnectionState is a wrapper around a bool. It returns the current state of the internet connection. At any time, it is either true or false.

Syntax:

ConnectionState(bool isConnected: false)

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')),
    );
  }
}
2
likes
80
pub points
0%
popularity

Publisher

verified publisherpeuconomia.com

The library helps to check for internet conenctivity in all platforms supported by Flutter. It provides a stream to continuously listen for internet conenction state and a future to check for internet connection.

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (LICENSE)

Dependencies

flutter, freezed_annotation, http, json_annotation

More

Packages that depend on connection_info