API Reference
Complete reference for the public API of ocm_ads, imported with package:ocm_ads/ocm_ads.dart.
📦 OcmFlutterPlugin
Static entry point for initializing and configuring the SDK.
initializeWithConfig()
Initializes the SDK with a local config. Call it once, before loading any ads.
static Future<void> initializeWithConfig(
OcmConfig config, {
String? prebidAccountId,
String? prebidServerUrl,
String? gdprConsentString,
bool? isGdprApplicable,
})
| Parameter | Type | Required | Description |
|---|---|---|---|
config | OcmConfig | Yes | Ad units, GAM settings, and privacy. See Configuration. |
prebidAccountId | String? | Android | Prebid Server account ID. Required on Android; on iOS, Prebid is skipped when it's omitted. |
prebidServerUrl | String? | No | Prebid Server endpoint. Android only. |
gdprConsentString | String? | No | TCF consent string. Android only. |
isGdprApplicable | bool? | No | Whether GDPR applies. Android only. |
Throws PlatformException with code missing_argument (no prebidAccountId on Android), invalid_config, or initialize_failed.
initialize()
Initializes the SDK with an OCM publisher ID, fetching the ad unit configuration remotely.
static Future<void> initialize({
required String publisherId,
String? gdprConsentString,
bool? isGdprApplicable,
String? prebidServerUrl,
})
| Parameter | Type | Required | Description |
|---|---|---|---|
publisherId | String | Yes | Your OCM publisher ID. |
gdprConsentString | String? | No | TCF consent string. Android only. |
isGdprApplicable | bool? | No | Whether GDPR applies. Android only. |
prebidServerUrl | String? | No | Overrides the default Prebid Server. Android only. |
Throws PlatformException with code missing_argument or initialize_failed.
setPrebidDebug()
static Future<void> setPrebidDebug(bool enabled)
Turns Prebid debug mode on or off for subsequent auction requests. Keep it disabled in production.
setPbsEventLogging()
static Future<void> setPbsEventLogging(bool enabled)
Turns logging of raw Prebid Server requests and responses on or off. Logs may contain request identifiers, so keep it disabled in production.
⚙️ Configuration Types
OcmConfig
const OcmConfig({
required List<OcmAdUnit> adUnits,
required OcmGamConfig gam,
required OcmPrivacyConfig privacy,
})
OcmAdUnit
const OcmAdUnit({
required String id,
required String format,
required String size,
required String position,
required int refresh,
OcmRenderMode renderMode = OcmRenderMode.prebidGam,
})
OcmGamConfig
const OcmGamConfig({required String networkCode, required String adUnitPath})
OcmPrivacyConfig
const OcmPrivacyConfig({required int gdpr, required String ccpa, required int coppa})
See Configuration for what each field means.
OcmRenderMode
| Value | Description |
|---|---|
prebidGam | Fetch demand from Prebid and render it through Google Ad Manager. Default. |
prebid | Fetch and render demand directly through Prebid Mobile. The ad unit id must be the Prebid configuration ID. |
OcmNativeLoadMode
| Value | Description |
|---|---|
gam | Load native demand through Google Ad Manager, using a GAM custom native format. Requires gamAdUnitId and gamNativeFormatId. Default. |
prebid | Load native demand directly from Prebid. |
🖼️ Banner Ads
OcmBannerView
A widget that shows a banner for an ad unit from the OCM configuration.
const OcmBannerView({
Key? key,
required String configId,
double? width,
double? height,
VoidCallback? onAdLoaded,
OcmAdFailureCallback? onAdFailed,
VoidCallback? onAdClicked,
VoidCallback? onImpression,
})
| Property | Description |
|---|---|
configId | The ad unit id from the OCM configuration. |
width, height | Banner size in logical pixels. |
onAdLoaded | Called when the banner loads. |
onAdFailed | Called with an OcmAdError when the banner fails to load. |
onAdClicked | Called when the user taps the banner. |
onImpression | Called when the banner records an impression. |
On platforms other than Android and iOS it renders an empty box and reports an unsupported_platform error through onAdFailed.
🎬 Interstitial Ads
OcmInterstitialAd
A full-screen interstitial. Load it, show it once isReady() returns true, and dispose it when it's no longer needed.
OcmInterstitialAd.load()
static Future<OcmInterstitialAd> load({
required String prebidConfigId,
required String gamAdUnitId,
Duration? fetchTimeout,
Duration? autoRefreshInterval,
OcmInterstitialListener? listener,
})
| Parameter | Type | Required | Description |
|---|---|---|---|
prebidConfigId | String | Yes | Prebid configuration ID for the interstitial. |
gamAdUnitId | String | Yes | GAM ad unit path, for example '/75351959/testadunit/test_app_interstitial'. |
fetchTimeout | Duration? | No | Limits the bid request. |
autoRefreshInterval | Duration? | No | Reloads the ad periodically. |
listener | OcmInterstitialListener? | No | Receives load and display events. |
Returns an OcmInterstitialAd handle as soon as loading starts. Use onLoaded or isReady() to know when it can be shown.
Instance methods
| Method | Returns | Description |
|---|---|---|
isReady() | Future<bool> | Whether the interstitial has loaded and can be shown. |
show() | Future<void> | Shows the interstitial. Throws no_activity if there's no visible Activity / view controller, or not_found if the ad was disposed. |
dispose() | Future<void> | Releases the native resources held by this ad. |
OcmInterstitialListener
Extend it and override only the events you need.
| Method | Description |
|---|---|
onLoaded() | The interstitial loaded. |
onFailedToLoad(OcmAdError error) | The interstitial failed to load. |
onShown() | The interstitial is shown. |
onClosed() | The user closed the interstitial. |
onClicked() | The user tapped the interstitial. |
🏆 Rewarded Ads
OcmRewardedAd
A full-screen rewarded ad. Load it, optionally call applyServerSideVerification, then show it and dispose it when it's no longer needed.
OcmRewardedAd.load()
static Future<OcmRewardedAd> load({
required String adUnitId,
OcmRewardedListener? listener,
})
| Parameter | Type | Required | Description |
|---|---|---|---|
adUnitId | String | Yes | Google Mobile Ads rewarded ad unit ID, for example 'ca-app-pub-3940256099942544/5224354917' (Google's Android test unit). |
listener | OcmRewardedListener? | No | Receives load, display, and reward events. |
Returns an OcmRewardedAd handle as soon as loading starts. Show it after onLoaded.
Instance methods
| Method | Returns | Description |
|---|---|---|
applyServerSideVerification({required String userId, String? customData}) | Future<void> | Sets the user ID and optional custom data sent with server-side verification callbacks. Call it before show(). |
show() | Future<void> | Shows the rewarded ad. Throws no_activity or not_found like interstitials. |
dispose() | Future<void> | Releases the native resources held by this ad. |
OcmRewardedListener
| Method | Description |
|---|---|
onLoaded() | The rewarded ad loaded. |
onFailedToLoad(OcmAdError error) | The rewarded ad failed to load. |
onShowFailed(OcmAdError error) | The rewarded ad failed to show. |
onDisplayed() | The rewarded ad is displayed. |
onDismissed() | The user dismissed the rewarded ad. |
onUserEarnedReward(OcmRewardedResult reward) | The user earned the reward. |
OcmRewardedResult
| Field | Type | Description |
|---|---|---|
adUnitId | String | The rewarded ad unit that granted the reward. |
rewardType | String | The reward type configured for the ad unit. |
rewardAmount | int | The reward amount. |
shownAtMillis | int | When the ad was shown, in milliseconds since the Unix epoch. |
📱 Native Ads
OcmNativeAdView
A widget that loads and renders a native ad, with impression and click tracking. Use it for every native ad shown to users.
const OcmNativeAdView({
Key? key,
required String prebidConfigId,
String? gamAdUnitId,
String? gamNativeFormatId,
OcmNativeLoadMode mode = OcmNativeLoadMode.gam,
double? width,
double? height,
VoidCallback? onAdLoaded,
OcmAdFailureCallback? onAdFailed,
})
| Property | Description |
|---|---|
prebidConfigId | Prebid configuration ID for the native placement. |
gamAdUnitId | GAM ad unit ID. Required in OcmNativeLoadMode.gam. |
gamNativeFormatId | GAM custom native format ID. Required in OcmNativeLoadMode.gam. |
mode | How the native bid is loaded and rendered. Defaults to OcmNativeLoadMode.gam. |
width | View width in logical pixels. |
height | View height in logical pixels. On Android, leave it null to size the view to the rendered ad. On iOS, give it a bounded height. |
onAdLoaded | Called when the native ad loads. |
onAdFailed | Called with an OcmAdError when the native ad fails to load. |
The view reloads when prebidConfigId, gamAdUnitId, gamNativeFormatId, or mode change. On iOS it also follows the app theme's brightness.
OcmNativeAdLoader (Android only)
Loads native ad data without rendering it. It does not register impression or click tracking, so use OcmNativeAdView for ads shown to users.
const OcmNativeAdLoader({OcmNativeListener? listener})
Future<OcmNativeAd> load({
required String prebidConfigId,
String? gamAdUnitId,
String? gamNativeFormatId,
OcmNativeLoadMode mode = OcmNativeLoadMode.gam,
})
The GAM IDs are required in OcmNativeLoadMode.gam. On Android, load throws missing_argument when they're missing, or native_load_failed. On iOS it throws unsupported_platform: the iOS SDK renders native ads as views and doesn't expose asset data.
OcmNativeListener
| Method | Description |
|---|---|
onLoaded(OcmNativeAd ad) | Called with the loaded native ad. |
onFailedToLoad(OcmAdError error) | The native ad failed to load. |
OcmNativeAd
Native ad assets returned by OcmNativeAdLoader. Every field may be null.
| Field | Type | Description |
|---|---|---|
title | String? | The headline. |
body | String? | The body text. |
publisherCompany | String? | The advertiser or publisher name. |
callToAction | String? | The call-to-action label, for example Install. |
imageUrl | String? | The main image URL. |
iconUrl | String? | The icon URL. |
videoUrl | String? | The video URL. |
layoutType | String? | The layout type reported by the SDK. |
⚠️ Error Handling
The plugin reports errors in two ways:
- Method calls (
initializeWithConfig,load,show, …) throw aPlatformExceptionfrompackage:flutter/services.dart. - Ad callbacks and listeners receive an
OcmAdError.
OcmAdError
| Field | Type | Description |
|---|---|---|
code | String? | A machine-readable error code, when the SDK provides one. |
message | String | A human-readable description of the error. |
OcmAdFailureCallback is void Function(OcmAdError error).
Error Codes
| Code | When |
|---|---|
missing_argument | A required argument is missing, for example prebidAccountId in initializeWithConfig on Android, or the GAM IDs for a GAM native load. |
invalid_config | The OcmConfig couldn't be parsed. |
initialize_failed | The native SDK failed to initialize. |
no_activity | show() was called with no visible Activity (Android) or view controller (iOS). |
not_found | The interstitial or rewarded ad handle doesn't exist, for example after dispose(). |
native_load_failed | OcmNativeAdLoader.load failed on Android. |
unsupported_platform | The feature isn't available on this platform: OcmNativeAdLoader on iOS, ad widgets outside Android and iOS, or the iOS SDK wasn't linked (see Installation). |
import 'package:flutter/services.dart';
try {
await OcmFlutterPlugin.initializeWithConfig(
config,
prebidAccountId: 'your-prebid-account-id',
);
} on PlatformException catch (error) {
debugPrint('OCM error: ${error.code} ${error.message}');
}
Next Steps
- Check out complete Examples