network_type_detector 2.0.0 copy "network_type_detector: ^2.0.0" to clipboard
network_type_detector: ^2.0.0 copied to clipboard

This package allows your Flutter app to detect the current network type, supporting 2G, 3G, 4G, 5G, and Wi-Fi.

example/lib/main.dart

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

import 'package:flutter/services.dart';
import 'package:network_type_detector/network_type_detector.dart';

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

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

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

class _MyAppState extends State<MyApp> {
  String _networkStatus = 'Unknown';
  String _networkStatusStream = 'Unknown';
  late final StreamSubscription _networkStatusSubscription;
  final _networkTypeDetectorPlugin = NetworkTypeDetector();

  @override
  void initState() {
    super.initState();
    initPlatformState();
    _networkStatusSubscription =
        _networkTypeDetectorPlugin.onNetworkStateChanged.listen((event) {
      setState(() {
        _networkStatusStream = event.toString();
      });
    });
  }

  @override
  void dispose() {
    _networkStatusSubscription.cancel();
    super.dispose();
  }

  Future<void> initPlatformState() async {
    String status;
    try {
      status =
          (await _networkTypeDetectorPlugin.currentNetworkStatus()).toString();
    } on PlatformException {
      status = 'Failed to get platform version.';
    }
    if (!mounted) return;

    setState(() {
      _networkStatus = status;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Column(
            children: [
              Text('Running on: $_networkStatus\n'),
              Text('Running on Change: $_networkStatusStream'),
            ],
          ),
        ),
      ),
    );
  }
}
3
likes
160
points
522
downloads

Documentation

API reference

Publisher

verified publishertatsuyuki25.com

Weekly Downloads

This package allows your Flutter app to detect the current network type, supporting 2G, 3G, 4G, 5G, and Wi-Fi.

Repository (GitHub)
View/report issues

License

BSD-3-Clause (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on network_type_detector

Packages that implement network_type_detector