Skip to content

fix(flet-ads): make ad controls constructable — move platform guard to before_update - #6735

Merged
FeodorFitsner merged 6 commits into
mainfrom
fix/ads-platform-guard-6726
Jul 29, 2026
Merged

fix(flet-ads): make ad controls constructable — move platform guard to before_update#6735
FeodorFitsner merged 6 commits into
mainfrom
fix/ads-platform-guard-6726

Conversation

@ndonkoHenri

@ndonkoHenri ndonkoHenri commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Fixes #6726

Problem

Since 0.85, constructing any flet_ads control crashes immediately:

RuntimeError: InterstitialAd(N) Control must be added to the page first

This hits BannerAd, InterstitialAd, NativeAd, and ConsentManager — the whole package is unusable.

Cause

The mobile-only guard reads self.page, but #6615 moved it into init() — which runs at construction, before the control is mounted, so the parent-chain lookup for self.page raises. For services it can never resolve there.

Fix

Move the guard back to before_update() (its pre-#6615 home), a post-mount hook where self.page resolves. Ads now construct freely and only raise FletUnsupportedPlatformException at mount on web/desktop.

Summary by Sourcery

Fix flet-ads controls so they can be constructed without crashing by deferring the mobile-only platform guard to a post-mount lifecycle hook and document the fix in the changelog.

Bug Fixes:

  • Prevent flet-ads controls and ConsentManager from crashing on construction by running the mobile-only platform guard after the control is attached to the page.

Documentation:

  • Document the flet-ads construction crash fix and its scope in the 0.86.4 changelog entry.

…structable

The mobile-only guard read self.page inside init(), which runs at construction
before the control is mounted, so self.page raised "must be added to the page
first" for every ad (BannerAd, InterstitialAd, NativeAd, ConsentManager). Move
it back to before_update(), a post-mount hook where self.page resolves.

Fixes #6726

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jul 28, 2026

Copy link
Copy Markdown

Deploying flet-website-v2 with  Cloudflare Pages  Cloudflare Pages

Latest commit: 4550cde
Status: ✅  Deploy successful!
Preview URL: https://19929b51.flet-website-v2.pages.dev
Branch Preview URL: https://fix-ads-platform-guard-6726.flet-website-v2.pages.dev

View logs

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a regression in flet-ads where constructing ad-related controls/services would immediately crash because a mobile-only platform guard accessed self.page too early (during init()), before the control/service was mounted into a page.

Changes:

  • Move the mobile-only platform guard from init() to before_update() for BaseAd and ConsentManager so instances can be constructed safely and validation happens post-mount.
  • Add a changelog entry documenting the regression and the fix.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
sdk/python/packages/flet-ads/src/flet_ads/consent_manager.py Defers platform guard to before_update() to avoid construction-time self.page resolution.
sdk/python/packages/flet-ads/src/flet_ads/base_ad.py Defers shared ad platform guard to before_update() so all ads become constructible pre-mount.
CHANGELOG.md Documents the construction-time crash regression and the lifecycle-hook fix.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread sdk/python/packages/flet-ads/src/flet_ads/consent_manager.py Outdated
ndonkoHenri and others added 2 commits July 28, 2026 12:42
The mobile-only guard now runs in before_update() (at mount), not per method
call, so the docstring no longer claims the exception is raised "when any of
its methods are called". Matches the wording used by the other ad controls.
Addresses Copilot review feedback on #6735.
@FeodorFitsner
FeodorFitsner merged commit ff3a09f into main Jul 29, 2026
3 of 148 checks passed
@FeodorFitsner
FeodorFitsner deleted the fix/ads-platform-guard-6726 branch July 29, 2026 17:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: flet-ads not working in 0.86

3 participants