fix(flet-ads): make ad controls constructable — move platform guard to before_update - #6735
Merged
Conversation
…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
Deploying flet-website-v2 with
|
| 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 |
# Conflicts: # CHANGELOG.md
Contributor
There was a problem hiding this comment.
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()tobefore_update()forBaseAdandConsentManagerso 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.
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
approved these changes
Jul 29, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #6726
Problem
Since 0.85, constructing any
flet_adscontrol crashes immediately:This hits
BannerAd,InterstitialAd,NativeAd, andConsentManager— the whole package is unusable.Cause
The mobile-only guard reads
self.page, but #6615 moved it intoinit()— which runs at construction, before the control is mounted, so the parent-chain lookup forself.pageraises. For services it can never resolve there.Fix
Move the guard back to
before_update()(its pre-#6615 home), a post-mount hook whereself.pageresolves. Ads now construct freely and only raiseFletUnsupportedPlatformExceptionat 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:
Documentation: