Skip to content
Private betaTreebars is letting teams in from the waitlist.Join the waitlist

Flutter

One package for iOS and Android. It runs the native Treebars SDKs underneath, so a Flutter app sends, retries, keeps sessions and draws messages exactly as a native one does.

1. Install

Flutter 3.24 or later, iOS 15.1 or later, and Android API 24 or later.

bash
flutter pub add treebars_flutter_sdk

On iOS, raise the app's target to 15.1 in ios/Podfile. A new Flutter app starts lower, and the install refuses the plugin until it matches:

ios/Podfileruby
platform :ios, '15.1'

2. Initialise

Before runApp, with your write key. Signed in, the sample below carries your own; every key is also under Settings → Write keys.

dart
import 'package:treebars_flutter_sdk/treebars_flutter_sdk.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await treebars.init(const TreebarsConfig(
    writeKey: '<your-write-key>',
    backendUrl: 'https://ingest.treebars.com',
  ));
  runApp(const MyApp());
}

The highlighted ids above are blanks until you sign in.

3. Send events

dart
await treebars.identify('user_123', attributes: {'plan': 'pro'});
await treebars.track('signup_completed', {'plan': 'pro'});
await treebars.screen('checkout');

await treebars.reset(); // when they sign out

Every call returns a Future, and calls reach the SDK in the order you make them: an event tracked right after identify belongs to the person who just signed in, and one tracked right after reset to nobody. A call the SDK refuses throws a TreebarsException whose code says why.

App opens, sessions, screens going to the background and the device's details are sent by themselves, as on every SDK. Pass null as the user id to set attributes without signing anybody in.

Push

Register the token your push plugin gives you, with the provider it came from. On iOS an APNs token is the device token written as hex. The credentials go on the dashboard, as Push setup describes.

dart
await treebars.requestPushPermission();
await treebars.registerPushToken(token, Platform.isIOS ? PushProvider.apns : PushProvider.fcm);

Every tap on a Treebars push reaches the SDK by itself — in front, in the background, or when the tap starts the app. Route its link where your app routes the links it is opened with:

dart
treebars.on('pushOpened', (data) {
  final link = (data as Map?)?['link'] as String?;
  if (link != null) router.go(Uri.parse(link).path);
});

A tap that started the app is held until you listen. While anything listens, the SDK does not open the link itself; with nothing listening, it opens it with the system. More in Deep links.

In-app messages

The SDK fetches and draws in-app messages itself; there is nothing to render in Dart. Hold them back while something must not be covered, and say which screen is showing so a message can be aimed at it:

dart
await treebars.setCurrentContext(['checkout']);
await treebars.holdInApp();    // during a video or a payment
await treebars.releaseInApp();

A button can fire a named action your app handles: treebars.onAction('add_to_cart', (action) => cart.add(action.payload['sku'])). The notification inbox is treebars.notifications().

Remote config

dart
await treebars.configReady(); // on the first launch, wait up to two seconds for published values

final newCheckout = await treebars.getConfigBoolean('new_checkout', false);
final headline = await treebars.getConfigString('home_headline', 'Welcome');

A read answers from what the device already holds and never waits for the network. How a key keeps its answer, and what the fallback means, is in Remote config.

Hand every link the app is opened with — from app_links or your router — to the SDK, and open the path it answers. Add linkHosts (your link domains) and, for the screen after an install on iPhone, deferredHandoff: true to TreebarsConfig.

dart
final path = await treebars.handleLink(url);   // null: route the URL as before
if (path != null) router.go(path);

// The screen a link was for, after an install from it:
treebars.onDeferredDeepLink((path) => router.go(path));

Purchases from your server

Just before a purchase, ask for a context token and send it with the checkout request; your server reports the purchase with it, and the sale lands on this phone and this session. See Server events.

dart
final treebarsContext = await treebars.contextToken();   // null: go on without it

Signed sign-ins

Where an environment requires it, pass the signature your backend made for the account id:

dart
await treebars.identify(user.id, attributes: {'email': user.email}, signature: signature);

Drawing a message yourself

The SDK draws every in-app message itself. A campaign marked self-handled is handed to your app instead, to draw in your own widgets; report what happened to it so its numbers stay true.

dart
final messages = await treebars.getSelfHandledInApps();
final id = messages.first['delivery_id'] as String;
await treebars.selfHandledShown(id);
await treebars.selfHandledClicked(id);     // or selfHandledDismissed(id)

Privacy

dart
await treebars.optOut();       // nothing is recorded until optIn()
await treebars.wipeLocalData(); // forget this device

Asking for tracking consent? Start with acquisitionConsent: false in TreebarsConfig, then call await treebars.setAcquisitionConsent(true) once they agree.

Both work as they do on every SDK; see Privacy controls.