---
metadata:
  - name: generator
    content: Diplodoc Platform v5.52.0
alternate:
  - https://ads.yandex.com/helpcenter/en/easy/integration/flutter/formats/app-open-ads.md
  - https://ads.yandex.com/helpcenter/ru/easy/integration/flutter/formats/app-open-ads.md
  - https://ads.yandex.com/helpcenter/zh/easy/integration/flutter/formats/app-open-ads.md
  - href: ru/easy/integration/flutter/formats/app-open-ads.md
    type: text/markdown
    title: Markdown version
  - href: ../llms.txt
    type: text/markdown
    title: llms.txt
---
> **Documentation Index:** Fetch the complete configuration index at https://ads.yandex.com/helpcenter/ru/llms.txt

# Реклама при открытии приложения

<!-- source: ru/easy/_includes/notes-flutter-easy-guide.md -->
{% note info %}

Пример работы всех типов форматов есть в [демопроекте](https://ads.yandex.com/helpcenter/ru/easy/integration/flutter/testing.md#demo).

{% endnote %}
<!-- endsource: ru/easy/_includes/notes-flutter-easy-guide.md -->


#|
||

**Сущность**

|

**Описание**

||
||

`adUnitId`

|

Используйте:

- **development mode** — для работы с [демоблоками](https://ads.yandex.com/helpcenter/ru/easy/integration/flutter/testing.md#blocks);

- **production mode** — для работы с `R-M-XXXXXX-Y` (уточните реальный ID в интерфейсе Рекламной сети Яндекса). `R-M-XXXXXX-Y` — это вид рабочего рекламного ID, по которому будут приходить разные креативы.

||
|#

## Пример создания рекламы при открытии приложения

```dart
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:yandex_mobileads/mobile_ads.dart';

class AppOpenPage extends StatefulWidget {
  const AppOpenPage({super.key});

  @override
  State<AppOpenPage> createState() => _AppOpenPageState();
}

class _AppOpenPageState extends State<AppOpenPage> with WidgetsBindingObserver {
  static const _tag = 'AppOpen';
  static const _adUnitId = 'demo-appopenad-yandex';

  final AppOpenAdLoader _loader = AppOpenAdLoader();
  AppOpenAd? _ad;
  String _status = 'Initializing...';
  bool _wasInBackground = false;
  static bool _isShowing = false;

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
    _init();
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    _ad?.destroy();
    super.dispose();
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    debugPrint('[$_tag] Lifecycle: $state');
    if (state == AppLifecycleState.paused) {
      _wasInBackground = true;
    } else if (state == AppLifecycleState.resumed && _wasInBackground && _ad != null && !_isShowing) {
      _wasInBackground = false;
      _show();
    }
  }

  Future<void> _init() async {
    setState(() => _status = 'Loading ad...');
    try {
      final ad = await _loader.loadAd(
        adRequest: AdRequest(adUnitId: _adUnitId),
      );
      if (!mounted) {
        ad.destroy();
        return;
      }
      _ad = ad;
      setState(() => _status = 'Ready!\n\nMinimize app and return to show ad');
    } on AdRequestError catch (error) {
      debugPrint('[$_tag] onAdFailedToLoad: ${error.description}');
      if (mounted) setState(() => _status = 'Error: ${error.description}');
    }
  }

  Future<void> _show() async {
    final ad = _ad;
    if (ad == null || _isShowing) return;
    _isShowing = true;

    ad.setAdEventListener(
      eventListener: AppOpenAdEventListener(
        onAdShown: () {
          debugPrint('[$_tag] onAdShown');
          if (mounted) setState(() => _status = 'Showing...');
        },
        onAdDismissed: () {
          debugPrint('[$_tag] onAdDismissed');
          _isShowing = false;
          _ad?.destroy();
          _ad = null;
          if (mounted) setState(() => _status = 'Closed');
        },
        onAdClicked: () => debugPrint('[$_tag] onAdClicked'),
        onAdFailedToShow: (error) {
          debugPrint('[$_tag] onAdFailedToShow: ${error.description}');
          _isShowing = false;
          if (mounted) setState(() => _status = 'Show error: ${error.description}');
        },
        onAdImpression: (ImpressionData impressionData) => debugPrint('[$_tag] onAdImpression: $impressionData'),
      ),
    );

    await ad.show();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('AppOpen')),
      body: Center(
        child: Padding(
          padding: const EdgeInsets.all(24),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              if (_status.contains('Loading') || _status.contains('Creating'))
                const CircularProgressIndicator(),
              const SizedBox(height: 16),
              Text(_status, textAlign: TextAlign.center),
            ],
          ),
        ),
      ),
    );
  }
}

```

## Проверка интеграции

<!-- source: ru/easy/_includes/notes-flutter-easy-guide.md -->
Соберите и запустите проект. Успешную интеграцию можно проверить в **Logcat** **Android Studio** по ключевому слову `YandexAds`:
<!-- endsource: ru/easy/_includes/notes-flutter-easy-guide.md -->

```
[Integration] Ad type App Open was integrated successfully
```
