Skip to main content

flutter_dns_adblocker 0.0.1 copy "flutter_dns_adblocker: ^0.0.1" to clipboard
flutter_dns_adblocker: ^0.0.1 copied to clipboard

A Flutter package for DNS-based ad blocking in WebViews using flutter_inappwebview. Block ads by filtering DNS requests with comprehensive domain blocklists.

flutter_dns_adblocker #

A powerful Flutter package for DNS-based ad blocking in WebViews using flutter_inappwebview. Block ads, trackers, and unwanted content by filtering DNS requests with comprehensive domain blocklists.

Features #

✨ DNS-Based Ad Blocking - Block ads at the DNS level before they even load
🛡️ Comprehensive Blocklist - 100+ popular ad networks and trackers pre-configured
⚙️ Highly Configurable - Custom blocklists, whitelists, and fine-grained control
📊 Real-time Statistics - Track blocked ads count in real-time
🎯 Easy Integration - Drop-in replacement for InAppWebView
🔒 Privacy-Focused - Block tracking scripts and analytics
📱 Cross-Platform - Works on iOS, Android, macOS, Windows, and Web

Installation #

Add this to your package's pubspec.yaml file:

dependencies:
  flutter_dns_adblocker: ^0.0.1

Then run:

flutter pub get

Platform-Specific Setup #

iOS #

Add the following to your ios/Runner/Info.plist:

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

Android #

Add the following to your android/app/src/main/AndroidManifest.xml:

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

Set minimum SDK version to 19 in android/app/build.gradle:

minSdkVersion 19

Usage #

Basic Usage #

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

class MyWebView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return AdBlockingWebView(
      initialUrl: 'https://www.example.com',
      config: AdBlockerConfig(
        enabled: true,
        trackBlockedAds: true,
      ),
      onAdBlocked: (url, totalBlocked) {
        print('Blocked ad: $url');
        print('Total blocked: $totalBlocked');
      },
    );
  }
}

Advanced Configuration #

AdBlockingWebView(
  initialUrl: 'https://www.example.com',
  config: AdBlockerConfig(
    enabled: true,
    trackBlockedAds: true,
    debugMode: true, // Enable debug logging
    customBlocklist: [
      'custom-ad-network.com',
      'another-tracker.com',
    ],
    whitelist: [
      'trusted-domain.com', // Never block this domain
    ],
  ),
  onAdBlocked: (url, totalBlocked) {
    print('Blocked: $url (Total: $totalBlocked)');
  },
  onLoadStart: (url) {
    print('Loading: $url');
  },
  onLoadStop: (url) {
    print('Loaded: $url');
  },
  onLoadError: (url, code, message) {
    print('Error: $message');
  },
)

Toggle Ad Blocking #

class MyWebViewPage extends StatefulWidget {
  @override
  _MyWebViewPageState createState() => _MyWebViewPageState();
}

class _MyWebViewPageState extends State<MyWebViewPage> {
  bool _adBlockingEnabled = true;
  int _blockedCount = 0;

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        SwitchListTile(
          title: Text('Ad Blocking'),
          subtitle: Text('Blocked: $_blockedCount ads'),
          value: _adBlockingEnabled,
          onChanged: (value) {
            setState(() {
              _adBlockingEnabled = value;
              _blockedCount = 0;
            });
          },
        ),
        Expanded(
          child: AdBlockingWebView(
            key: ValueKey(_adBlockingEnabled),
            initialUrl: 'https://www.example.com',
            config: AdBlockerConfig(enabled: _adBlockingEnabled),
            onAdBlocked: (url, total) {
              setState(() => _blockedCount = total);
            },
          ),
        ),
      ],
    );
  }
}

Configuration Options #

AdBlockerConfig #

Parameter Type Default Description
enabled bool true Enable/disable ad blocking
customBlocklist List<String> [] Additional domains to block
whitelist List<String> [] Domains to never block
trackBlockedAds bool true Track blocked ads count
debugMode bool false Enable debug logging

Blocked Ad Networks #

The package blocks 100+ popular ad networks and trackers, including:

  • Google Ads - googlesyndication.com, googleadservices.com, doubleclick.net
  • Facebook Ads - facebook.com/tr, connect.facebook.net
  • Amazon Ads - amazon-adsystem.com, amazonclix.com
  • Twitter Ads - ads-twitter.com, analytics.twitter.com
  • AdMob - admob.com, app-measurement.com
  • Analytics - google-analytics.com, mixpanel.com, segment.com
  • And many more...

See ad_domains.dart for the complete list.

How It Works #

The package uses multiple techniques to block ads:

  1. Content Blockers (iOS/macOS) - Uses WKWebView's native content blocking API
  2. Request Interception (Android) - Intercepts and blocks requests to ad domains
  3. Navigation Blocking - Prevents navigation to ad URLs
  4. Resource Blocking - Blocks loading of ad resources (images, scripts, etc.)

Example App #

Check out the example directory for a complete demo app that shows:

  • URL input with quick links to test sites
  • Toggle ad blocking on/off
  • Real-time blocked ads counter
  • Loading indicators
  • Error handling

Run the example:

cd example
flutter run

Testing Ad Blocking #

To test the ad blocker, try loading ad-heavy websites like:

  • News sites (CNN, Forbes, The Verge)
  • Tech blogs (TechCrunch, Engadget)
  • Social media sites

You should see significantly fewer ads and faster page loads!

Performance #

  • Minimal overhead - Blocking happens at the request level
  • Fast page loads - Ads are blocked before they download
  • Low memory usage - No heavy processing or regex matching
  • Battery efficient - Less network activity = better battery life

Privacy #

This package enhances privacy by blocking:

  • Ad tracking pixels
  • Analytics scripts
  • Social media trackers
  • Third-party cookies
  • Fingerprinting scripts

Limitations #

  • Server-side ads - Cannot block ads served from the same domain as content
  • First-party ads - Ads served directly by the website may not be blocked
  • Dynamic domains - New ad networks may not be in the blocklist yet

Contributing #

Contributions are welcome! To add new ad domains to the blocklist:

  1. Fork the repository
  2. Add domains to lib/src/data/ad_domains.dart
  3. Run tests: flutter test
  4. Submit a pull request

License #

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

Support #

If you find this package helpful, please give it a ⭐ on GitHub!

For issues and feature requests, please file them on the GitHub issue tracker.

Changelog #

See CHANGELOG.md for version history.

1
likes
140
points
35
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

A Flutter package for DNS-based ad blocking in WebViews using flutter_inappwebview. Block ads by filtering DNS requests with comprehensive domain blocklists.

Repository (GitHub)
View/report issues

License

MIT (license)

Dependencies

flutter, flutter_inappwebview

More

Packages that depend on flutter_dns_adblocker