network_info 1.0.2
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