network_info 1.0.2 copy "network_info: ^1.0.2" to clipboard
network_info: ^1.0.2 copied to clipboard

A standalone Flutter package for retrieving network information (public and local IP addresses)

Network Info Package #

A standalone Flutter package for retrieving network information including public and local IP addresses.

Features #

  • ๐ŸŒ Get public IP address (visible on the internet)
  • ๐Ÿ  Get local IP address (LAN/Wi-Fi)
  • ๐Ÿ”„ Multiple fallback services for reliability
  • ๐Ÿงช Fully tested with 100+ test cases
  • ๐Ÿ—๏ธ Clean Architecture with SOLID principles
  • ๐Ÿ’‰ Dependency injection support with GetIt
  • ๐ŸŽฏ Type-safe with strong typing
  • ๐Ÿ“ฆ Zero dependencies on other custom packages

Installation #

Add to your pubspec.yaml:

dependencies:
  network_info:
    path: packages/network_info

Quick Start #

1. Setup Dependency Injection #

import 'package:network_info/network_info.dart';

void main() {
  // Initialize the package
  NetworkInfoDI.setupNetworkInfoDI();
  
  runApp(MyApp());
}

2. Use in Your Code #

import 'package:get_it/get_it.dart';
import 'package:network_info/network_info.dart';

final locator = GetIt.instance;

Future<void> getNetworkInfo() async {
  final networkInfo = locator<INetworkInfo>();
  
  // Get public IP
  final publicIp = await networkInfo.getPublicIp();
  print('Public IP: $publicIp');
  
  // Get local IP
  final localIp = await networkInfo.getLocalIp();
  print('Local IP: $localIp');
  
  // Get network info model
  final info = await networkInfo.getNetworkInfo();
  print('Public: ${info.publicIp}, Local: ${info.localIp}');
}

Architecture #

This package follows Clean Architecture and SOLID principles with clear separation of concerns:

lib/
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ domain/              # Business logic layer (pure Dart)
โ”‚   โ”‚   โ”œโ”€โ”€ models/
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ network_info_model.dart
โ”‚   โ”‚   โ”œโ”€โ”€ repository/
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ i_network_info_repository.dart
โ”‚   โ”‚   โ””โ”€โ”€ exceptions/
โ”‚   โ”‚       โ””โ”€โ”€ network_info_exception.dart
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ data/                # Data layer (implementation)
โ”‚   โ”‚   โ”œโ”€โ”€ config/
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ network_info_config.dart
โ”‚   โ”‚   โ”œโ”€โ”€ data_sources/
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ public_ip_data_source.dart
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ local_ip_data_source.dart
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ connectivity_data_source.dart
โ”‚   โ”‚   โ””โ”€โ”€ repository/
โ”‚   โ”‚       โ””โ”€โ”€ network_info_repository_impl.dart
โ”‚   โ”‚
โ”‚   โ””โ”€โ”€ di/                  # Dependency injection
โ”‚       โ””โ”€โ”€ network_info_di.dart
โ”‚
โ””โ”€โ”€ network_info.dart        # Public API

SOLID Principles Applied #

  • Single Responsibility: Each class has one clear responsibility
  • Open/Closed: Easy to extend without modifying existing code
  • Liskov Substitution: All implementations can be substituted with their interfaces
  • Interface Segregation: Focused interfaces with only necessary methods
  • Dependency Inversion: High-level modules depend on abstractions

Design Patterns Used #

  • Repository Pattern: Abstracts data source details
  • Dependency Injection: For better testability and flexibility
  • Strategy Pattern: Multiple implementations for different scenarios

Comparison with Other Packages #

Feature This Package network_info_plus connectivity_plus
Public IP โœ… Multiple services โŒ โŒ
Local IP โœ… โœ… โŒ
Connectivity โœ… โŒ โœ…
Clean Architecture โœ… โŒ โŒ
DI Support โœ… GetIt โŒ โŒ
Testability โœ… Full mocking โš ๏ธ Limited โš ๏ธ Limited
SOLID Principles โœ… โŒ โŒ
Retry Logic โœ… Configurable โŒ โŒ
Fallback Services โœ… Multiple โŒ โŒ

Future Enhancements #

  • IPv6 Support: Add IPv6 address retrieval
  • Caching: Add configurable caching for public IP
  • Network Speed: Add network speed testing
  • VPN Detection: Detect if VPN is active
  • Proxy Detection: Detect proxy usage
  • Network Type: Detailed network type (2G/3G/4G/5G)

Advanced Usage #

Custom HTTP Client #

import 'package:http/http.dart' as http;

NetworkInfoDI.setupNetworkInfoDI(
  customHttpClient: http.Client(),
);

Custom Configuration #

NetworkInfoDI.setupNetworkInfoDI(
  config: NetworkInfoConfig(
    publicIpServices: [
      'https://api.ipify.org?format=text',
      'https://icanhazip.com',
    ],
    timeout: Duration(seconds: 10),
  ),
);

Error Handling #

try {
  final publicIp = await networkInfo.getPublicIp();
  if (publicIp == null) {
    print('Unable to retrieve public IP (offline or service unavailable)');
  }
} on NetworkInfoException catch (e) {
  print('Error: ${e.message}');
}

Testing #

The package includes comprehensive tests:

# Run all tests
cd packages/network_info
flutter test

# Run with coverage
flutter test --coverage

# Generate mocks
dart run build_runner build

SOLID Principles #

  • Single Responsibility: Each class has one clear purpose
  • Open/Closed: Extensible through interfaces
  • Liskov Substitution: All implementations are substitutable
  • Interface Segregation: Small, focused interfaces
  • Dependency Inversion: Depends on abstractions, not concretions

Author #

Sepehr Tabeian

License #

This project is licensed under the MIT License - see the LICENSE file for details.

Copyright (c) 2025 Sepehr Tabeian

2
likes
150
points
22
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

A standalone Flutter package for retrieving network information (public and local IP addresses)

Repository (GitHub)
View/report issues

License

MIT (license)

Dependencies

connectivity_plus, flutter, get_it, http

More

Packages that depend on network_info