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

A Flutter plugin for managing internet permissions and checking network connectivity. Check if device is connected to internet, get connection type (WiFi, Mobile, Ethernet, VPN).

internet_permission #

pub package likes popularity pub points

A lightweight Flutter plugin that helps you check internet connectivity and automatically manages network permissions.

✨ Features #

  • ✅ Check if device is connected to internet
  • 🔌 Get connection type (WiFi, Mobile, Ethernet, VPN)
  • 🚀 Automatically adds permissions (no manual setup needed!)
  • 📱 Cross-platform (Android & iOS)
  • 🎯 Null-safe
  • ⚡ Easy to use

📦 Installation #

Add to your pubspec.yaml:

dependencies:
  internet_permission: ^1.0.1

Then run:

flutter pub get

⚙️ Setup #

Android - Automatic! 🎉 #

Good news: Permissions are added automatically via Android's manifest merger!

Just install the package - that's it! No manual setup needed.

The plugin's manifest includes:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

These will be automatically merged into your app's manifest when you build.

Verify Auto-Merge (Optional)

Want to confirm it worked? After building, check:

android/app/build/intermediates/merged_manifests/debug/AndroidManifest.xml

You'll see the permissions are there! ✅

Manual Setup (Fallback)

If auto-merge doesn't work for some reason, you can add manually to android/app/src/main/AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    
    <application ...>
        ...
    </application>
</manifest>

iOS - No Setup Needed! 🍎 #

iOS allows internet by default.

For HTTP support (optional): Add to ios/Runner/Info.plist:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

⚠️ Only enable HTTP if absolutely necessary. Use HTTPS when possible.

🚀 Quick Start #

import 'package:internet_permission/internet_permission.dart';

final internetPermission = InternetPermission();

// Check if connected
bool isConnected = await internetPermission.isConnected();
if (isConnected) {
  print('✅ Connected to internet');
} else {
  print('❌ No internet connection');
}

// Get connection type
String type = await internetPermission.getConnectionType();
print('Connection type: $type'); // wifi, mobile, ethernet, vpn, or none

📖 Usage Examples #

Check Connection Before API Call #

Future<void> fetchData() async {
  final internetPermission = InternetPermission();
  
  if (!await internetPermission.isConnected()) {
    throw Exception('No internet connection');
  }
  
  // Make your API call
  final response = await http.get(Uri.parse('https://api.example.com'));
  ...
}

Show Offline Message #

bool isConnected = await internetPermission.isConnected();

if (!isConnected) {
  ScaffoldMessenger.of(context).showSnackBar(
    SnackBar(content: Text('No internet connection')),
  );
  return;
}

Adjust Based on Connection Type #

String type = await internetPermission.getConnectionType();

if (type == 'mobile') {
  // Load lower quality images to save data
  loadLowQualityImages();
} else if (type == 'wifi') {
  // Load high quality images
  loadHighQualityImages();
}

Complete Example App #

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

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final _plugin = InternetPermission();
  String _status = 'Checking...';
  String _type = 'Unknown';

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

  Future<void> _checkConnection() async {
    bool connected = await _plugin.isConnected();
    String type = await _plugin.getConnectionType();
    
    setState(() {
      _status = connected ? '✅ Connected' : '❌ Disconnected';
      _type = type;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text('Internet Permission Demo')),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text(_status, style: TextStyle(fontSize: 24)),
              SizedBox(height: 20),
              Text('Type: $_type', style: TextStyle(fontSize: 18)),
              SizedBox(height: 40),
              ElevatedButton(
                onPressed: _checkConnection,
                child: Text('Refresh'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

📚 API Reference #

InternetPermission #

Methods

Method Returns Description
isConnected() Future<bool> Check if device is connected to internet
getConnectionType() Future<String> Get connection type: wifi, mobile, ethernet, vpn, none, or unknown
hasInternetPermission() Future<bool> Check if internet permission is granted
getPlatformVersion() Future<String?> Get platform version (for debugging)

Connection Types #

  • wifi - Connected via WiFi
  • mobile - Connected via cellular data
  • ethernet - Connected via ethernet cable
  • vpn - Connected via VPN
  • none - No connection
  • unknown - Unable to determine

🔍 Troubleshooting #

Android: Permissions not auto-merged? #

This is rare, but if auto-merge fails:

  1. Clean and rebuild

    flutter clean
    flutter pub get
    flutter run
    
  2. Check merged manifest

    android/app/build/intermediates/merged_manifests/debug/AndroidManifest.xml
    
  3. Add manually (as fallback - see Setup section above)

  4. Check Gradle version Make sure you're using Gradle 7.0+ and Android Gradle Plugin 7.0+

iOS: HTTP Not Working? #

Add ATS exception to Info.plist (see Setup section).

Plugin Not Recognized? #

flutter clean
flutter pub get
cd ios && pod install && cd ..
flutter run

More Help? #

  • 📖 Testing Guide - How to verify auto-merge works
  • 📖 Installation Guide
  • 🔧 Troubleshooting Guide
  • 🐛 Report Issues

❓ FAQ #

Q: Do I need to add permissions manually?
A: No! Permissions are added automatically via manifest merger.

Q: What if auto-merge doesn't work?
A: Rare, but you can add permissions manually as a fallback. See Setup section.

Q: Does this work on iOS?
A: Yes! iOS allows internet by default, so nothing to configure.

Q: Can I check the merged manifest?
A: Yes! Build your app and check android/app/build/intermediates/merged_manifests/debug/AndroidManifest.xml

📋 Requirements #

  • Flutter: >=3.3.0
  • Dart: >=3.0.0 <4.0.0
  • Android: minSdkVersion 21 (Android 5.0)
  • iOS: 11.0+
  • Gradle: 7.0+ (for auto-merge)

🤝 Contributing #

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

📝 License #

MIT License - see the LICENSE file for details.

💖 Support #

If you find this package useful, please:

  • ⭐ Star it on GitHub
  • 👍 Like it on pub.dev
  • 📢 Share it with others

👨‍💻 Author #

Abubakr


Made with ❤️ for the Flutter community

3
likes
0
points
378
downloads

Publisher

unverified uploader

Weekly Downloads

A Flutter plugin for managing internet permissions and checking network connectivity. Check if device is connected to internet, get connection type (WiFi, Mobile, Ethernet, VPN).

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on internet_permission

Packages that implement internet_permission