cross_connectivity 1.0.2

  • Readme
  • Changelog
  • Example
  • Installing
  • 90

cross_connectivity #

Build Pub GitHub GitHub stars

A Flutter plugin for handling Connectivity and REAL Connection state in the mobile, web and desktop platforms. Supports iOS, Android, Web, Windows, Linux and macOS.

Getting Started #

In order to use this plugin, add dependency in the pubspec.yaml:

cross_connectivity: ^1.0.1

or

cross_connectivity:
    git:
      url: https://github.com/marchdev-tk/cross_connectivity

Add an import to dart file:

import 'package:cross_connectivity/cross_connectivity.dart';

Samples #

Web sample:

Web Sample

Desktop sample:

Desktop Sample

Mobile sample:

Mobile Sample

Usage #

Functional approach

This plugin provides two streams:

  • isConnected that shows whether the device is REALLY connected to the network or not.
  • onConnectivityChanged that it will not let you know about state of the REAL network connection. It only shows connectivity state.

Also for one time check could be used following methods:

  • checkConnection() that is working like isConnected, but returns Future<bool> instread of Stream<bool>.
  • checkConnectivity() that is working like onConnectivityChanged, but returns Future<ConnectivityStatus> instread of Stream<ConnectivityStatus>.

As an addition there are more methods (they are working only on Android/iOS/macOS):

  • getWifiName() - Obtains the wifi name (SSID) of the connected network.
  • getWifiBSSID() - Obtains the wifi BSSID of the connected network.
  • getWifiIP() - Obtains the IP address of the connected wifi network.

Widget approach

As an alteration to funcitonal approach could be used ConnectivityBuilder widget as follows:

ConnectivityBuilder(
  builder: (context, isConnected, status) => Row(
    mainAxisSize: MainAxisSize.min,
    children: <Widget>[
      Icon(
        isConnected == true
            ? Icons.signal_wifi_4_bar
            : Icons.signal_wifi_off,
        color: isConnected == true ? Colors.green : Colors.red,
      ),
      const SizedBox(width: 8),
      Text(
        '$status',
        style: TextStyle(
          color: status != ConnectivityStatus.none
              ? Colors.green
              : Colors.red,
        ),
      ),
    ],
  ),
)

Feature requests and Bug reports #

Feel free to post a feature requests or report a bug here.

Changelog #

[1.0.2] #

  • Removed debug printing.

[1.0.1] #

  • Fixed incompatibility issue with Safari and IE of NetworkInformation API.

[1.0.0] #

  • Created common interface for handling Connectivity and REAL Connection state in the mobile, web and desktop platforms. Supports iOS, Android, Web, Windows, Linux and macOS.

example/lib/main.dart

// Copyright (c) 2020, the MarchDev Toolkit project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:flutter/material.dart';

import 'package:cross_connectivity/cross_connectivity.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Cross Connectivity Example',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Cross Connectivity Example'),
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Center(
            child: ConnectivityBuilder(
              builder: (context, isConnected, status) => Row(
                mainAxisSize: MainAxisSize.min,
                children: <Widget>[
                  Icon(
                    isConnected == true
                        ? Icons.signal_wifi_4_bar
                        : Icons.signal_wifi_off,
                    color: isConnected == true ? Colors.green : Colors.red,
                  ),
                  const SizedBox(width: 8),
                  Text(
                    '$status',
                    style: TextStyle(
                      color: status != ConnectivityStatus.none
                          ? Colors.green
                          : Colors.red,
                    ),
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  cross_connectivity: ^1.0.2

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:cross_connectivity/cross_connectivity.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
81
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
90
Learn more about scoring.

We analyzed this package on Jul 9, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.14
  • Flutter: 1.17.5

Analysis suggestions

Package does not support Flutter platform linux

Because:

  • package:cross_connectivity/cross_connectivity.dart that imports:
  • package:cross_connectivity/src/connectivity.io.dart that imports:
  • package:cross_connectivity/src/core/connectivity_service.common.dart that imports:
  • package:connectivity/connectivity.dart that declares support for platforms: android, ios, macos, web

Package does not support Flutter platform windows

Because:

  • package:cross_connectivity/cross_connectivity.dart that imports:
  • package:cross_connectivity/src/connectivity.io.dart that imports:
  • package:cross_connectivity/src/core/connectivity_service.common.dart that imports:
  • package:connectivity/connectivity.dart that declares support for platforms: android, ios, macos, web

Package not compatible with SDK dart

Because:

  • cross_connectivity that is a package requiring null.

Health suggestions

Format lib/src/core/connectivity_service.web.dart.

Run flutter format to format lib/src/core/connectivity_service.web.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
connectivity ^0.4.8+6 0.4.9
connectivity_macos ^0.1.0+3 0.1.0+3
flutter 0.0.0
http ^0.12.1 0.12.1
meta ^1.1.8 1.1.8 1.2.1
rxdart ^0.24.1 0.24.1
Transitive dependencies
charcode 1.1.3
collection 1.14.12 1.14.13
connectivity_for_web 0.3.0
connectivity_platform_interface 1.0.6
flutter_web_plugins 0.0.0
http_parser 3.1.4
js 0.6.2
path 1.7.0
plugin_platform_interface 1.0.2
sky_engine 0.0.99
source_span 1.7.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6 1.2.0
vector_math 2.0.8 2.1.0-nullsafety
Dev dependencies
flutter_test
pedantic ^1.8.0 1.9.0 1.9.1