flutter_adsense

pub version Flutter Web Wasm compatible

Flutter Web library to initialize, manage, and display Google AdSense ads.

Built with package:web + dart:js_interop β€” fully compatible with both the JavaScript and WebAssembly compilation targets.


Features

πŸš€ Zero dart:html Uses package:web + dart:js_interop β€” Wasm-ready.
🧩 Single widget Drop AdsenseWidget anywhere in your layout.
πŸ›‘οΈ Safe on all platforms Conditional exports provide no-op stubs on mobile/desktop.
⏱️ Correct push timing Uses addPostFrameCallback to push the ad only after the <ins> element is live in the DOM.
πŸ”’ Singleton core FlutterAdsense injects the script only once, regardless of how many times initialize() is called.

Installation

dependencies:
  flutter_adsense: ^1.0.0
flutter pub get

Setup

If you use AdSense Auto Ads, add the script tag to your web/index.html inside <head>. Alternatively, skip this step and use only the AdsenseWidget for manual placement.

<!-- web/index.html β€” inside <head> -->
<script async
  src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-XXXXXXXXXXXXXXXX"
  crossorigin="anonymous">
</script>

2. Initialize in main.dart

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

void main() {
  // Injects adsbygoogle.js into <head> exactly once.
  FlutterAdsense().initialize('ca-pub-XXXXXXXXXXXXXXXX');

  runApp(const MyApp());
}

3. Display ad units

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

class MyPage extends StatelessWidget {
  const MyPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          // Your content…
          const Text('Hello World'),

          // Leaderboard banner
          const AdsenseWidget(
            adClient: 'ca-pub-XXXXXXXXXXXXXXXX',
            adSlot: '1234567890',
            width: 728,
            height: 90,
          ),
        ],
      ),
    );
  }
}

AdsenseWidget parameters

Parameter Type Default Description
adClient String required Your AdSense Publisher ID (ca-pub-…)
adSlot String required The ad block / slot ID
adFormat String 'auto' AdSense format: 'auto', 'rectangle', 'vertical', 'horizontal'
fullWidthResponsive bool true Allow the ad to stretch to container width
width double required Width in logical pixels
height double required Height in logical pixels

Common ad sizes

Name Width Height
Leaderboard 728 90
Medium Rectangle 300 250
Large Rectangle 336 280
Half Page 300 600
Mobile Banner 320 50

Notes

  • This package targets Flutter Web only. On mobile/desktop it compiles safely via no-op stubs, but ads will not be shown.
  • AdSense policies require your site to be approved before ads serve.
  • During development you will see an empty white space where the ad would appear β€” this is expected.

Author

Developed by Wandson Dev.

Libraries

flutter_adsense
Flutter AdSense β€” Google AdSense for Flutter Web.