Adapty SDK for Flutter

repository·master·Indexed 41 days ago

https://github.com/adaptyteam/adaptysdk-flutter

An open-source framework for simplifying in-app subscription and purchase integration in Flutter applications. Features include no-code flow building, A/B testing for paywalls, server-side receipt validation, and the AdaptyUI system for presenting paywalls and onboarding flows. Supports Flutter 3.32.0+, Dart 3.8.0+, iOS 15.0, and Android API 21.

Tokens
7.4K
Snippets
21
Records
29
Agent score
99%

What's inside Adapty

  1. Customize iOS Launch Screen Assets

    master

    To change the launch screen image for the iOS version of your Flutter app, you can either replace the image files directly in the example/ios/Runner/Assets.xcassets/LaunchImage.imageset/ directory or use Xcode for a visual approach.

    Using Xcode:

    1. Open your project's iOS workspace using open ios/Runner.xcworkspace.
    2. In the Xcode Project Navigator, navigate to Runner/Assets.xcassets.
    3. Drag and drop your desired images into the asset catalog to replace the existing ones.
    open ios/Runner.xcworkspace
  2. Configure Swift Package Manager for iOS

    master

    As of version 4.0.0, the Adapty iOS native SDK is distributed exclusively via Swift Package Manager (CocoaPods support has been dropped).

    • Flutter 3.44+: SwiftPM is enabled by default, and the plugin works out of the box.
    • Flutter 3.32–3.43: You must manually enable Swift Package Manager for your project before building for iOS by running the following command.
    flutter config --enable-swift-package-manager
  3. Understand the AdaptyProfile model

    master

    The AdaptyProfile class represents the current state of a user within the Adapty ecosystem. It contains identifiers, custom attributes, access levels, and purchase history (subscriptions and non-subscriptions). This object is the primary way to inspect a user's entitlements and metadata in your Flutter app.

    Key properties include:

    • profileId: The unique identifier for the user in Adapty.
    • customerUserId: An optional identifier from your own system.
    • customAttributes: A map of user-defined attributes previously set via .updateProfile().
    • accessLevels: A map where keys are access level identifiers (configured in the Adapty Dashboard) and values are AdaptyAccessLevel objects.
    • subscriptions: A map of store product IDs to AdaptySubscription information.
    • nonSubscriptions: A map of store product IDs to lists of AdaptyNonSubscription (consumables) information.
    • appliedAttributionSources: A list of AdaptyAttributionSource values used for segmentation.
    • isTestUser: A boolean indicating if the profile belongs to a test device.
  4. Handle AdaptyUI flow events

    master

    You can listen to events occurring within an active flow (like button clicks or navigation) using two methods:

    1. Global Observer: Use setFlowsEventsObserver to set a single AdaptyUIFlowsEventsObserver that listens to all flow events.
    2. View-Specific Listener: Use registerFlowEventsListener to attach an AdaptyUIFlowsEventsObserver to a specific viewId.

    To prevent memory leaks, always call unregisterFlowEventsListener(viewId) or pass null to setFlowsEventsObserver when the listener is no longer needed.

    // Register a listener for a specific view
    AdaptyUI().registerFlowEventsListener(myObserver, 'my_unique_view_id');
    
    // Unregister when done
    AdaptyUI().unregisterFlowEventsListener('my_unique_view_id');
  5. Create and present an AdaptyUIFlowView

    master

    To display a paywall or onboarding flow designed in the Adapty Flow Builder, first obtain an AdaptyFlow object. Use createFlowView to generate an AdaptyUIFlowView instance, then call presentFlowView to show it to the user.

    Key Parameters for createFlowView:

    • flow: The AdaptyFlow object to render.
    • locale: (Optional) The localization identifier (e.g., en, es). Falls back to the flow's default if the requested locale is unavailable.
    • preloadProducts: If true, the SDK prefetches required products during view assembly.
    • androidEnableSafeArea: (Android only) If true, applies safe-area insets as paddings. Defaults to true.
    • productPurchaseParams: A map of AdaptyProductIdentifier to AdaptyPurchaseParameters for specific product configurations.

    Presentation Options: When calling presentFlowView, you can specify iosPresentationStyle (e.g., AdaptyUIIOSPresentationStyle.fullScreen).

    // 1. Create the view
    final flowView = await AdaptyUI().createFlowView(
      flow: myAdaptyFlow,
      locale: 'en',
      preloadProducts: true,
    );
    
    // 2. Present the view
    await AdaptyUI().presentFlowView(flowView);
    
    // 3. Dismiss when finished
    await AdaptyUI().dismissFlowView(flowView);
  6. Deprecated: Onboarding Feature

    master

    As of Adapty SDK 4.0.0, the standalone Onboarding Feature is deprecated. Developers should migrate to using Flows for all onboarding and paywall experiences.

    Deprecated methods include:

    • createOnboardingView / presentOnboardingView / dismissOnboardingView
    • registerOnboardingEventsListener / unregisterOnboardingEventsListener
    • setOnboardingsEventsObserver
  7. Present or dismiss an AdaptyUIFlowView

    master

    Once you have obtained an AdaptyUIFlowView instance (typically via AdaptyUI.createFlowView), you can manage its visibility directly through the view object.

    • Use .present() to show the flow view. On iOS, you can specify the presentation style using AdaptyUIIOSPresentationStyle.
    • Use .dismiss() to close the currently presented flow view.
    // To present the view
    await flowView.present(iosPresentationStyle: AdaptyUIIOSPresentationStyle.fullScreen);
    
    // To dismiss the view
    await flowView.dismiss();
  8. Configure the Adapty SDK with AdaptyConfiguration

    master

    Use AdaptyConfiguration to initialize the SDK with your API key and customize its behavior. The configuration follows a builder-like pattern where you instantiate the object with a required apiKey and then call various with... methods to set specific properties.

    Key configuration options include:

    • Customer Identity: Use withCustomerUserId to associate a user in your system with Adapty. You can optionally provide an iosAppAccountToken (UUID string) or an androidObfuscatedAccountId.
    • Observer Mode: Use withObserverMode(true) if you handle purchases and subscription status manually and only use Adapty for analytics and event reporting.
    • Privacy & Collection: Methods like withAppleIdfaCollectionDisabled, withGoogleAdvertisingIdCollectionDisabled, and withIpAddressCollectionDisabled allow you to control data collection for compliance.
    • Server Cluster: Use withServerCluster to specify the backend cluster (defaultCluster, eu, or cn).
    • Logging: Use withLogLevel to set the AdaptyLogLevel.
    final configuration = AdaptyConfiguration(apiKey: 'YOUR_API_KEY')
      ..withCustomerUserId('user_123', iosAppAccountToken: 'UUID-HERE')
      ..withObserverMode(false)
      ..withLogLevel(AdaptyLogLevel.debug)
      ..withServerCluster(AdaptyServerCluster.eu);
  9. Understand the AdaptyNonSubscription model

    master

    The AdaptyNonSubscription class represents a non-subscription (one-time or consumable) purchase processed through Adapty. It contains metadata about the transaction, the store used, and the product details. This model is useful for tracking one-time products and ensuring you don't process the same purchaseId multiple times.

    // Example of the fields available in an AdaptyNonSubscription object
    final String purchaseId;          // Unique identifier for the purchase in Adapty
    final String store;               // 'app_store', 'play_store', or 'adapty'
    final String vendorProductId;    // The product ID from the store
    final String? vendorTransactionId; // The transaction ID from the store (nullable)
    final DateTime purchasedAt;       // Timestamp of purchase
    final bool isSandbox;            // True if purchased in a sandbox environment
    final bool isRefund;             // True if the purchase was refunded
    final bool isConsumable;          // True if the product is consumable
  10. Open URLs and request App Reviews

    master

    The AdaptyUI class provides utility methods for common mobile interactions:

    • openUrl(String url, {AdaptyWebPresentation openIn}): Opens a URL using either an external browser or an in-app browser. This is the default handler for the open_url action in flows.
    • requestAppReview(): Triggers the native OS app review prompt (e.g., StoreKit on iOS). Note that this is a 'best-effort' call; the operating system ultimately decides whether to show the prompt to the user.
    // Open a URL in an in-app browser
    await AdaptyUI().openUrl(
      'https://example.com',
      openIn: AdaptyWebPresentation.inAppBrowser,
    );
    
    // Request native app review
    await AdaptyUI().requestAppReview();