Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flet Ads in andriod #4722

Open
1 task done
mostafakk100 opened this issue Jan 16, 2025 · 9 comments
Open
1 task done

Flet Ads in andriod #4722

mostafakk100 opened this issue Jan 16, 2025 · 9 comments

Comments

@mostafakk100
Copy link

Duplicate Check

Describe the bug

i try build a app with ads and my id_banner flet build apk --include-packages flet_ads --android-meta-data com.google.android.gms.ads.APPLICATION_ID="ca-app-pub-1327011022394480~5377121235" -vv
and this is my code

Code sample

Code
import flet as ft
import flet.ads as ads


def main(page: ft.Page):
    page.horizontal_alignment = ft.CrossAxisAlignment.CENTER

    # استبدال المعرّفات بمعلوماتك الحقيقية
    id_interstitial = (
        "ca-app-pub-1327011022394480/4993682708"  # الإعلان البيني
    )

    id_banner = (
        "ca-app-pub-1327011022394480/3983853363"  # الإعلان البانر
    )

    def handle_interstitial_close(e):
        nonlocal iad
        print("InterstitialAd closed")
        page.overlay.remove(e.control)
        page.overlay.append(iad := get_new_interstitial_ad())
        page.update()

    def get_new_interstitial_ad():
        return ads.InterstitialAd(
            unit_id=id_interstitial,
            on_load=lambda _: print("InterstitialAd loaded"),
            on_error=lambda e: print("InterstitialAd error", e.data),
            on_open=lambda _: print("InterstitialAd opened"),
            on_close=handle_interstitial_close,
            on_impression=lambda _: print("InterstitialAd impression"),
            on_click=lambda _: print("InterstitialAd clicked"),
        )

    def display_new_banner_ad():
        page.add(
            ft.Container(
                content=ads.BannerAd(
                    unit_id=id_banner,
                    on_click=lambda _: print("BannerAd clicked"),
                    on_load=lambda _: print("BannerAd loaded"),
                    on_error=lambda e: print("BannerAd error", e.data),
                    on_open=lambda _: print("BannerAd opened"),
                    on_close=lambda _: print("BannerAd closed"),
                    on_impression=lambda _: print("BannerAd impression"),
                    on_will_dismiss=lambda _: print("BannerAd will dismiss"),
                ),
                width=320,
                height=50,
                bgcolor=ft.colors.TRANSPARENT,
            )
        )

    page.overlay.append(iad := get_new_interstitial_ad())
    page.appbar = ft.AppBar(
        adaptive=True,
        title=ft.Text("Mobile Ads Playground"),
        bgcolor=ft.colors.LIGHT_BLUE_300,
    )
    page.add(
        ft.OutlinedButton("Show InterstitialAd", on_click=lambda _: iad.show()),
        ft.OutlinedButton("Show BannerAd", on_click=lambda _: display_new_banner_ad()),
    )


ft.app(main)

To reproduce

and after i run my app in my phone i see this

Image
my app dont work can you help me ?

Expected behavior

No response

Screenshots / Videos

Captures

[Upload media here]

Operating System

Windows

Operating system details

windows 22H2 , andriod 14

Flet version

0.25.2

Regression

Yes, it used to work in a previous Flet version (please specify the version in additional details)

Suggestions

No response

Logs

Logs
[Paste your logs here]

Additional details

No response

@zox47
Copy link

zox47 commented Jan 16, 2025

Duplicate Check

Describe the bug

i try build a app with ads and my id_banner flet build apk --include-packages flet_ads --android-meta-data com.google.android.gms.ads.APPLICATION_ID="ca-app-pub-1327011022394480~5377121235" -vv and this is my code

Code sample

Code
import flet as ft
import flet.ads as ads

def main(page: ft.Page):
page.horizontal_alignment = ft.CrossAxisAlignment.CENTER

# استبدال المعرّفات بمعلوماتك الحقيقية
id_interstitial = (
    "ca-app-pub-1327011022394480/4993682708"  # الإعلان البيني
)

id_banner = (
    "ca-app-pub-1327011022394480/3983853363"  # الإعلان البانر
)

def handle_interstitial_close(e):
    nonlocal iad
    print("InterstitialAd closed")
    page.overlay.remove(e.control)
    page.overlay.append(iad := get_new_interstitial_ad())
    page.update()

def get_new_interstitial_ad():
    return ads.InterstitialAd(
        unit_id=id_interstitial,
        on_load=lambda _: print("InterstitialAd loaded"),
        on_error=lambda e: print("InterstitialAd error", e.data),
        on_open=lambda _: print("InterstitialAd opened"),
        on_close=handle_interstitial_close,
        on_impression=lambda _: print("InterstitialAd impression"),
        on_click=lambda _: print("InterstitialAd clicked"),
    )

def display_new_banner_ad():
    page.add(
        ft.Container(
            content=ads.BannerAd(
                unit_id=id_banner,
                on_click=lambda _: print("BannerAd clicked"),
                on_load=lambda _: print("BannerAd loaded"),
                on_error=lambda e: print("BannerAd error", e.data),
                on_open=lambda _: print("BannerAd opened"),
                on_close=lambda _: print("BannerAd closed"),
                on_impression=lambda _: print("BannerAd impression"),
                on_will_dismiss=lambda _: print("BannerAd will dismiss"),
            ),
            width=320,
            height=50,
            bgcolor=ft.colors.TRANSPARENT,
        )
    )

page.overlay.append(iad := get_new_interstitial_ad())
page.appbar = ft.AppBar(
    adaptive=True,
    title=ft.Text("Mobile Ads Playground"),
    bgcolor=ft.colors.LIGHT_BLUE_300,
)
page.add(
    ft.OutlinedButton("Show InterstitialAd", on_click=lambda _: iad.show()),
    ft.OutlinedButton("Show BannerAd", on_click=lambda _: display_new_banner_ad()),
)

ft.app(main)

To reproduce

and after i run my app in my phone i see this

Image my app dont work can you help me ?

Expected behavior

No response

Screenshots / Videos

Captures
[Upload media here]

Operating System

Windows

Operating system details

windows 22H2 , andriod 14

Flet version

0.25.2

Regression

Yes, it used to work in a previous Flet version (please specify the version in additional details)

Suggestions

No response

Logs

Logs
[Paste your logs here]

Additional details

No response

me too the same problem

@FeodorFitsner FeodorFitsner mentioned this issue Jan 16, 2025
1 task
@mostafakk100
Copy link
Author

do you know how can fix this?

@zox47
Copy link

zox47 commented Jan 18, 2025

just add
WidgetsFlutterBinding.ensureInitialized();
in main.dart inside main void

@mostafakk100
Copy link
Author

can you make a video about this because i dont know how can i fix it

@Harfho
Copy link

Harfho commented Jan 19, 2025

just add
WidgetsFlutterBinding.ensureInitialized();
in main.dart inside main void

@FeodorFitsner
@ndonkoHenri

@InfoRoute2
Copy link

I am with the same problem. I used the code 'flet build apk --include-packages flet_ads' to build the apk that run successfully without ads, but when I add adbanner in to the code and had the application built again to it get running into the android phone the app doesn't open

@geceyzlm
Copy link

geceyzlm commented Jan 22, 2025

Bunu nasıl düzeltebilirim biliyor musunuz?

can you make a video about this because i dont know how can i fix it

Did you solve it

@InfoRoute2
Copy link

Guys, I have been trying to fix an error of ad error in my application since 10/01. Tried to change a lot of things, but nothing works

@InfoRoute2
Copy link

Bunu nasıl düzeltebilirim biliyor musunuz?

can you make a video about this because i dont know how can i fix it

Did you solve it

I need to know too

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

No branches or pull requests

6 participants
@Harfho @zox47 @mostafakk100 @InfoRoute2 @geceyzlm and others