lesnitsky / network_state

🌎 Service aware network state plugin

Home Page:https://pub.dev/packages/network_state

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

network_state

Service aware network state plugin for Flutter

Sends HEAD requests to your service to make sure it is available

Support me

GitHub stars Twitter Follow

Motivation

Sometimes even though other packages like connectivity tell, that connection is OK, your WiFi may have limited network access, so you might want to make sure your service is available

Installation

pubspec.yaml

dependencies:
  network_state: ^0.0.1

NetworkState

NetworkState.startPolling();

final ns = new NetworkState();

ns.addListener(() async {
    final hasConnection = await ns.isConnected;
});

NetworkConfig

void main() {
    NetworkConfig.pingUrls = [
        'http://yourapi.com/ping',
        'http://yourotherapi.com/ping',
    ];
    // optional poll interval, defaults to 500
    NetworkConfig.pollIntervalMs = 300;
    // optional timeout, defaults to 500
    NetworkConfig.timeoutMs = 1000;

    runApp(MyApp);
}

NetworkStateBuilder

void main() {
    // ...

    NetworkState.startPolling();
    runApp(MyApp);
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Network State Demo',
      home: Scaffold(
        body: NetworkStateBuilder(
          builder: (BuildContext context, AsyncSnapshot snapshot) {
            return Center(
              child: Text(
                snapshot.hasData
                    ? 'Has connection: ${snapshot.data}'
                    : 'Loading...',
              ),
            );
          },
        ),
      ),
    );
  }
}

License

MIT

Support me

GitHub stars Twitter Follow

About

🌎 Service aware network state plugin

https://pub.dev/packages/network_state

License:MIT License


Languages

Language:Dart 82.2%Language:Objective-C 12.0%Language:Java 5.8%