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

outdated

This plugin allows Flutter applications to detect network changes. You can know the detailed mobile network types such as 2G, 3G, 4G, 5G. This plug-in is suitable for iOS and Android

example/lib/main.dart

import 'dart:io';

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:network_type_reachability/network_type_reachability.dart';
import 'package:permission_handler/permission_handler.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({Key key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {

  String _networkStatus = 'Unknown';
  StreamSubscription<NetworkStatus> subscription;

  @override
  void initState() {
    super.initState();
    _listenNetworkStatus();
  }

  _listenNetworkStatus()async {
    if(Platform.isAndroid) {
      await Permission.phone.request();
    }
    subscription = NetworkTypeReachability().onNetworkStateChanged.listen((event) {

      setState(() {
        _networkStatus = "${event}";
      });
    });
  }

  _currentNetworkStatus() async {
    if(Platform.isAndroid) {
      await Permission.phone.request();
    }
    NetworkStatus status = await NetworkTypeReachability().currentNetworkStatus();
    switch(status) {
      case NetworkStatus.unreachable:
      //unreachable
      case NetworkStatus.wifi:
      //wifi
      case NetworkStatus.mobile2G:
      //2g
      case NetworkStatus.moblie3G:
      //3g
      case NetworkStatus.moblie4G:
      //4g
      case NetworkStatus.moblie5G:
      //5h
      case NetworkStatus.otherMoblie:
      //other
    }
    setState(() {
      _networkStatus = status.toString() ;
    });
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    subscription.cancel();
  }


  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Text('Running on: $_networkStatus\n'),
        ),
      ),
    );
  }
}
11
likes
0
points
60
downloads

Publisher

verified publisheralecodeando.com

Weekly Downloads

This plugin allows Flutter applications to detect network changes. You can know the detailed mobile network types such as 2G, 3G, 4G, 5G. This plug-in is suitable for iOS and Android

Homepage

License

unknown (license)

Dependencies

flutter

More

Packages that depend on network_type_reachability

Packages that implement network_type_reachability