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

1
likes
150
points
90
downloads

Publisher

unverified uploader

Weekly Downloads

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

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

connectivity_plus, flutter, get_it, http

More

Packages that depend on network_info