Flutter SDK
See how easy it is to integrate high-performance ads into your Flutter app using our lightweight SDK.
Requirements
- Flutter: 3.24.0 or later
- Dart: 3.5.0 or later
- Android:
minSdkVersion >= 21
,compileSdk >= 34
, AGP version>= 7.3.0
(use Android Studio - Android Gradle plugin Upgrade Assistant for help), support forandroidx
(see AndroidX Migration to migrate an existing app) - iOS:
12.0+, --ios-language swift
, Xcode version>= 15.0
Getting started
WebView prerequisites (flutter_inappwebview
)
This SDK renders ads inside a WebView using flutter_inappwebview.
To avoid initialization issues, add this to your app entry point:
1. Installation
To get started, you will need to set up a publisher account to get a
publisherToken
and code
pubspec.yaml
:
2. Set up the Character object
First, provide information about the assistant’s character:3. Set up the Regulatory object
Next, provide information about regulations:4. Set up AdsProvider
Wrap your app (or the subtree that contains ad placements) with AdsProvider
.AdsProvider
fetches ads and needs access to the current chat messages
.
5. Display your first ad
An ad slot is a place in your UI where an ad is rendered. In most cases, this will be under a chat message. During onboarding, you receive acode
for each ad slot or ad format you want to show.
Example using the InlineAd
format:
💡 Note:InlineAd
does not always display an ad - whether an ad is shown depends on the context of the ongoing conversation. If there is no ad to display,InlineAd
automatically returns aconst SizedBox.shrink()
, so it won’t take up any extra space in your layout.
API documentation
AdsProvider
properties
Your unique publisher token.
List of messages between the assistant and the user.
Unique identifier that remains the same for the user’s lifetime (used for retargeting and rewarded ads).
Unique ID of the conversation.
Placement codes enabled for the conversation. Example:
['inlineAd']
.The character object used in a conversation.
Regulatory compliance information.
Device-specific identifier (IDFA on iOS, GAID on Android).
Vendor identifier (IDFV on iOS).
Publisher-provided identifier for the user cohort (for A/B testing).
iOS App Store ID for the app, used for better ad matching and reporting.
The log level for the SDK:
LogLevel.debug
, LogLevel.info
, LogLevel.log
, LogLevel.warn
, LogLevel.error
, or LogLevel.silent
.Used to pass publisher-specific information to Kontext. Contents will be discussed with your account manager if needed.
Flag indicating if the ads are disabled.
The URL of the ad server. Defaults to the default ad server URL if not provided.
Callback triggered when an event occurs.
InlineAd
properties
The ad format code that identifies the ad to be displayed.
A unique identifier for the message associated with this ad.
AdEvent types
ad.clicked
The user has clicked the ad.
ad.viewed
The user has viewed the ad.
ad.filled
Ad is available.
ad.no-fill
Ad is not available.
ad.render-started
Triggered before the first token is received.
ad.render-completed
Triggered after the last token is received.
ad.error
Triggered when an error occurs.
reward.granted
Triggered when the user receives a reward.
video.started
Triggered when the video playback starts.
video.completed
Triggered when the video playback finishes.
Guides
- Place
AdsProvider
high enough in the widget tree to cover all screens/areas that can show ads. - Keep the
messages
list up to date so the SDK can determine when and where to render ads.
Controlling when ads load
You can fine-tune ad frequency by controlling when newmessages
are passed into AdsProvider
. This allows you to adjust the ratio of ads to messages dynamically.
Handling no-fill events
You can notify when the ad is not available by using theonEvent
callback and listening for the ad.no-fill
event.
Troubleshooting
Missing plugin warnings
If you see warnings likeMissingPluginException
or errors about a plugin not being registered, try the following:
flutter run
or restarting your IDE.