---
metadata:
  - name: generator
    content: Diplodoc Platform v5.50.6
alternate:
  - https://ads.yandex.com/helpcenter/en/dev/flutter/release/6-0-0-migration.md
  - https://ads.yandex.com/helpcenter/ru/dev/flutter/release/6-0-0-migration.md
  - https://ads.yandex.com/helpcenter/zh/dev/flutter/release/6-0-0-migration.md
---
> **Documentation Index:** Fetch the complete configuration index at https://ads.yandex.com/helpcenter/zh/llms.txt

# 版本 6 迁移指南

{% note warning %}

确保将您使用的 [Yandex 聚合](https://ads.yandex.com/helpcenter/zh/dev/flutter/list-network.md) 适配器更新到最新版本。旧版本可能会导致适配器集成错误，从而导致您的广告无法投放。

{% endnote %}

## 横幅

将 `AdSize` 类重命名为 `BannerAdSize`。移除了 `FlexibleSize(int width, int height)` 方法。

根据广告类型使用方法：

使用 `BannerAdSize.stickySize(int width)` 方法创建自适应粘性横幅。

> 这是一个自动更新的小广告，放置在应用屏幕的顶部或底部。
>
> 广告与主要应用内容不重叠，常用于游戏应用中。
>
> 粘性横幅的高度是自动确定的，可根据设备的屏幕尺寸调整，并且不占用超过屏幕高度的 15%。

{% note warning %}

版本 6.0.0 添加了对自适应粘性横幅的自动刷新支持。

如果您之前已为粘性横幅实施了自动刷新，请禁用它。

{% endnote %}

使用 `BannerAdSize.inlineSize(int width, int maxHeight) method` 创建自适应内联横幅。

> 自适应内联横幅是一种灵活的横幅广告格式，通过优化每个设备上的广告尺寸，实现最大效率。
>
> 横幅的高度会自动调整，可能会达到设备屏幕的高度。
>
> 通常，这种格式在基于内容来源的应用或允许主要关注广告的上下文环境中使用。

## 激励广告

我们改变了创建和加载广告的方法。我们现在提供 `RewardedAdLoader` 加载器对象，用于加载广告，以及从 `OnAdLoaded` 广告加载事件检索的 `RewardedAd` 广告对象。

#|
||
广告加载
| **SDK 5**

用于广告加载和呈现的单个对象：

```dart
final ad = await RewardedAd.create(
  adUnitId: 'your-ad-unit-id',
  onAdLoaded: () {
    /* Do something */
  },
  onAdFailedToLoad: (error) {
      /* Do something */
  },
);
await ad.load(adRequest: AdRequest());
```

**SDK 6**

用于加载多个广告的 `RewardedAdLoader`：

```dart
late final Future<RewardedAdLoader> _adLoader =
      _createRewardedAdLoader();
RewardedAd? _ad;

Future<RewardedAdLoader> _createRewardedAdLoader() {
  return RewardedAdLoader.create(
    onAdLoaded: (RewardedAd rewardedAd) {
      // 广告加载成功。现在您可以显示已加载的广告
      _ad = rewardedAd;
    },
    onAdFailedToLoad: (error) {
      // 广告加载失败，出现 AdRequestError。
      // 强烈建议不要尝试通过 onAdFailedToLoad() 方法加载新广告。
    },
  );
}

Future<void> _loadRewardedAd() async {
  final adLoader = await _adLoader;
  await adLoader.loadAd(adRequestConfiguration: AdRequestConfiguration(adUnitId: 'your-ad-unit-id'));
}
```

||
||
广告呈现
| **SDK 5**

在展示广告之前，请查看广告是否已加载。

```dart
RewardedAd? _ad;

Future<void> _showRewardedAd() async {
  final ad = _ad;
  if (ad != null && ad.isLoaded) {
    await ad.show();
    var reward = await ad.waitForDismiss();
  }
}
```

**SDK 6**

您无需检查广告是否已加载。广告在通过 `onAdLoaded()` 回调方法返回后，准备好进行呈现。

```dart
RewardedAd? _ad;

Future<void> _showRewardedAd() async {
  final ad = _ad;
  if (ad != null) {
    _setAdEventListener(ad);
    await ad.show();
    var reward = await ad.waitForDismiss();
  }
}
```

||
||
订阅广告生命周期事件
| **SDK 5**

所有事件均到达单个 `RewardedAd` 对象。

```dart
RewardedAd.create(
  onAdLoaded: () {},
  onAdFailedToLoad: (AdLoadError error) {},
  onAdShown: () {},
  onAdDismissed: () {},
  onRewarded: (Reward reward) {},
  onAdClicked: () {},
  onLeftApplication: () {},
  onReturnedToApplication: () {},
  onImpression: (String? impressionData) {},
);
```

**SDK 6**

广告加载事件到达 `RewardedAdLoader` 对象，而广告呈现事件到达 `RewardedAdEventListener` 对象。

使用 `rewardedAd.setAdEventListener` 方法设置 `RewardedAdEventListener` 对象。

统一的事件名称。移除了 `OnLeftApplication` 和 `OnReturnedToApplication` 事件。

添加了 `onAdFailedToShow` 事件。

将 `onImpression` 事件重命名为 `onAdImpression`。

```dart
RewardedAdLoader.create(
  onAdLoaded: (RewardedAd rewardedAd) {},
  onAdFailedToLoad: (error) {},
);

void _setAdEventListener(RewardedAd ad) {
  ad.setAdEventListener(
      eventListener: RewardedAdEventListener(
          onAdShown: () {},
          onAdFailedToShow: (AdError error) {},
          onAdDismissed: () {},
          onAdClicked: () {},
          onAdImpression: (ImpressionData? data) {},
          onRewarded: (Reward reward) {}
      )
  );
}
```

||
|#

## 插屏广告

我们改变了创建和加载广告的方法。我们现在提供 `InterstitialAdLoader` 加载器对象，用于加载广告，以及从 `OnAdLoaded` 广告加载事件检索的 `Interstitial` 广告对象。

#|
||
广告加载
| **SDK 5**

用于广告加载和呈现的单个对象：

```dart
final ad = await InterstitialAd.create(
  adUnitId: 'your-ad-unit-id',
  onAdLoaded: () {
    /* Do something */
  },
  onAdFailedToLoad: (error) {
      /* Do something */
  },
);
await ad.load(adRequest: AdRequest());
```

**SDK 6**

用于加载多个广告的 `InterstitialAdLoader`。

```dart
late final Future<InterstitialAdLoader> _adLoader =
      _createInterstitialAdLoader();
InterstitialAd? _ad;

Future<InterstitialAdLoader> _createInterstitialAdLoader() {
  return InterstitialAdLoader.create(
    onAdLoaded: (InterstitialAd interstitialAd) {
      // 广告加载成功。现在您可以显示已加载的广告
      _ad = interstitialAd;
    },
    onAdFailedToLoad: (error) {
      // 广告加载失败，出现 AdRequestError。
      // 强烈建议不要尝试通过 onAdFailedToLoad() 方法加载新广告。
    },
  );
}

Future<void> _loadInterstitialAd() async {
  final adLoader = await _adLoader;
  await adLoader.loadAd(adRequestConfiguration: AdRequestConfiguration(adUnitId: 'your-ad-unit-id'));
}
```

||
||
广告呈现
| **SDK 5**

在展示广告之前，请查看广告是否已加载。

```dart
InterstitialAd? _ad;

Future<void> _showInterstitialAd() async {
  final ad = _ad;
  if (ad != null && ad.isLoaded) {
    await ad.show();
    await ad.waitForDismiss();
  }
}
```

**SDK 6**

您无需检查广告是否已加载。广告在通过 `onAdLoaded()` 回调方法返回后，准备好进行呈现。

```dart
InterstitialAd? _ad;

Future<void> _showInterstitialAd() async {
  final ad = _ad;
  if (ad != null) {
    _setAdEventListener(ad);
    await ad.show();
    await ad.waitForDismiss();
  }
}
```

||
||
订阅广告生命周期事件
| **SDK 5**

所有事件均到达单个 `InterstitialAd` 对象。

```dart
InterstitialAd.create(
  onAdLoaded: () {},
  onAdFailedToLoad: (AdLoadError error) {},
  onAdShown: () {},
  onAdDismissed: () {},
  onAdClicked: () {},
  onLeftApplication: () {},
  onReturnedToApplication: () {},
  onImpression: (String? impressionData) {},
);
```

**SDK 6**

广告加载事件到达 `InterstitialAdLoader` 对象，而广告呈现事件到达 `InterstitialAdEventListener` 对象。

使用 `interstitialAd.setAdEventListener` 方法设置 `InterstitialAdEventListener` 对象。

统一的事件名称。移除了 `OnLeftApplication` 和 `OnReturnedToApplication` 事件。

添加了 `onAdFailedToShow` 事件。

将 `onImpression` 事件重命名为 `onAdImpression`。

```dart
InterstitialAdLoader.create(
  onAdLoaded: (InterstitialAd interstitialAd) {},
  onAdFailedToLoad: (error) {},
);

void _setAdEventListener(InterstitialAd ad) {
  ad.setAdEventListener(
      eventListener: InterstitialAdEventListener(
          onAdShown: () {},
          onAdFailedToShow: (AdError error) {},
          onAdDismissed: () {},
          onAdClicked: () {},
          onAdImpression: (ImpressionData? data) {}
      )
  );
}
```

||
|#

## 开屏广告

添加了新的广告格式：开屏广告。如需了解更多信息，请参阅 [开屏广告](https://ads.yandex.com/helpcenter/zh/dev/flutter/app-open-ad.md)

您可以在此处查找完整的集成示例：

* <!-- source: zh/dev/_includes/github-pubdev-links.md -->
  [Pub.dev](https://pub.dev/packages/yandex_mobileads/example) 链接。
  <!-- endsource: zh/dev/_includes/github-pubdev-links.md -->
* <!-- source: zh/dev/_includes/github-pubdev-links.md -->
  [GitHub](https://github.com/yandexmobile/yandex-ads-flutter-plugin) 链接。
  <!-- endsource: zh/dev/_includes/github-pubdev-links.md -->

## Yandex 聚合

{% note warning %}

确保将您使用的 [Yandex 聚合](https://ads.yandex.com/helpcenter/zh/dev/flutter/list-network.md) 适配器更新到最新版本。旧版本可能会导致适配器集成错误，从而导致您的广告无法投放。

{% endnote %}

{% list tabs %}

- Android

   将 `com.yandex.ads.mediation:mobileads-admob` 构件重命名为 `com.yandex.ads.mediation:mobileads-google`。

   如果您使用标准化聚合构建，则无需执行任何操作。如果单独添加适配器，请替换项目中的 gradle 依赖项。

   #|
   || **SDK 5**
   ```gradle
   implementation 'com.yandex.ads.mediation:mobileads-admob:<version>'
   ```
   | **SDK 6**
   ```gradle
   implementation 'com.yandex.ads.mediation:mobileads-google:<version>'
   ```
   ||
   |#

- iOS

   我们将 `AdMobYandexMobileAdsAdapters` 适配器重命名为 `GoogleYandexMobileAdsAdapters`。如果您使用标准化聚合构建，则无需执行任何操作。如果单独添加适配器，则需要编辑项目的 Podfile。

   #|
   || **SDK 5**
   ```bash
   pod 'AdMobYandexMobileAdsAdapters'
   ```
   | **SDK 6**
   ```bash
   pod 'GoogleYandexMobileAdsAdapters'
   ```
   ||
   |#

{% endlist %}

