Flutter

Apptimize supports Flutter applications built for iOS and Android devices as a wrapper around the existing iOS and Android SDKs. You do not need to manage the installation of those SDKs, you need only add the plugin to your Flutter project.

SDK Installation

Install from pub.dev

The Apptimize flutter plugin is published on pub.dev as apptimize_flutter. The latest installation instructions for your version of flutter will always be displayed on the Installing tab.

  1. Update your dependencies in pubspec.yaml, adding apptimize_flutter.

    dependencies:
        apptimize_flutter: ^1.0    # Good, any 1.x version where x >= 0 works.
    
  2. Install it.

    • From the terminal: Run flutter pub get.

    OR

    • From Android Studio/IntelliJ: Click Packages get in the action ribbon at the top of pubspec.yaml.

    • From VS Code: Click Get Packages located in right side of the action ribbon at the top of pubspec.yaml.

  3. Import it. In each dart files you wish to use apptimize, add the following import statement:

    import 'package:apptimize_flutter/apptimize_flutter.dart';
    
  4. Stop and restart the app. This package brings platform-specific code for iOS and Android and that code must be built into your app. Hot reload and hot restart only update the Dart code, so a full restart of the app might be required to avoid errors like MissingPluginException when using the package.

  5. After that, sign in to the Apptimize dashboard and configure your tests.

You can find our latest Flutter Plugin Documentation here.

Install Directly

The SDK is also available as a downloadable package.

  1. Download the latest Flutter SDK from the SDK Download page.

  2. Copy the SDK to a new directory outside of your project, such as apptimize_flutter.

  3. Reference the SDK in your pubspec.yaml as follows:

    dependencies:
        apptimize_flutter:
            path: ../apptimize_flutter/
    
  4. Follow the rest of the directions in the SDK Installation section above.

Install from Git

The SDK is also available as a downloadable package. Reference the SDK in your pubspec.yaml as follows:

dependencies:
    apptimize_flutter:
        git: git://github.com/urbanairship/apptimize_flutter.git

Then follow the rest of the directions in the SDK Installation section above.

Initializing Apptimize

To initialize Apptimize, call the Apptimize.startApptimize() method with the App Key associated with your app, which can be found in the Install tab of your Dashboard.

Synchronous and Asynchronous Loading

To ensure the best experience for your end users, we recommend waiting to display your application content until Apptimize has been fully loaded and initialized. This will prevent situations where an outsized percentage of users are placed in the baseline variant because the experiment data hasn’t been fully received and processed by the SDK.

The sample code below shows how to use Apptimize.apptimizeInitializedStream to determine when Apptimize setup and initialization is complete.

Create an onApptimizeInitialized method.

// Create a function that will be executed whenever new
// Apptimize metadata is downloaded
Future<void> onApptimizeInitialized(ApptimizeInitializedEvent e) async {
    print("Apptimize setup and initialization complete");
    await Apptimize.track("Apptimize Initialized");

    // Set a flag to indicate Apptimize data is available
    // and content can be displayed
}

And put the following in your startup code.

Apptimize.apptimizeInitializedStream.listen((event) => {
    onApptimizeInitialized(event)
});
await Apptimize.startApptimize("<appkey>");

EU Cloud Site

Set the serverRegion property of the ApptimizeOptions class to EUCS and then pass this options instance to Apptimize.setup to set your Apptimize region to EUCS if you are integrated in our EU Cloud site.

var apptimizeOptions = new ApptimizeOptions();
apptimizeOptions.serverRegion = ApptimizeServerRegion.EUCS;

// Set additional options here...

await Apptimize.startApptimize(apptimizeAppKey, apptimizeOptions);

Troubleshooting

Cocoapods errors

[!] Automatically assigning platform `ios` with version `9.0` on target `Runner` because no platform
was specified. Please specify a platform for this target in your Podfile. See
`https://guides.cocoapods.org/syntax/podfile.html#platform`.