Flutter
Events, screens, sessions, revenue, feature flags and errors for Flutter apps on Android, iOS, the web and desktop.
The stancelens package records screen views, custom events, users, revenue, flag exposures and errors, keeps them in an offline queue and sends them in batches. It runs on Android, iOS, the web, macOS, Windows and Linux, and needs Flutter 3.19 (Dart 3.3) or later. The logic lives in stancelens_core, a pure Dart package with no Flutter dependency. Read Mobile apps for how sessions, screens and offline delivery work.
Install
pub.dev release pending; use the Git dependency. stancelens uses stancelens_core from the same repository, so the command also pins stancelens_core with a dependency override. Without the override, flutter pub get can't find stancelens_core.
flutter pub add 'stancelens:{"git":{"url":"https://github.com/stancedev/stancelens.git","ref":"main","path":"packages/flutter/stancelens"}}' 'override:stancelens_core:{"git":{"url":"https://github.com/stancedev/stancelens.git","ref":"main","path":"packages/flutter/stancelens_core"}}'Start
import 'package:stancelens/stancelens.dart';
import 'package:flutter/material.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Stancelens.init(
site: 'SITE_ID',
host: 'https://stancelens.com',
appVersion: '1.4.0 (42)', // optional, see "What is sent"
captureErrors: true,
);
runApp(MaterialApp(
navigatorObservers: [StancelensObserver()],
routes: {'/': (_) => const HomePage(), '/settings': (_) => const SettingsPage()},
));
}Call init once, before runApp. Calls made before it are ignored (debug builds print a message). The recording methods return a Future that completes when the event is queued and saved, not when it is sent; you don't need to await them.
Screens
StancelensObserver records a screen view when a page route becomes visible: on push, on replace, and when a pop shows the page below again. Dialogs and bottom sheets are not screens. The name is the route's RouteSettings.name (/settings stays /settings, Home becomes /Home); routes without a name are skipped.
// go_router: the GoRoute's name, or else its path as declared
GoRouter(observers: [StancelensObserver()], routes: [
GoRoute(name: 'User', path: '/users/:id', builder: ...), // sent as /User
]);
// A ShellRoute has its own navigator, so it needs its own observer
ShellRoute(observers: [StancelensObserver()], routes: [...]);
// Or record a screen yourself
Stancelens.screen('Checkout/Payment');API
| Call | What it does |
|---|---|
Stancelens.track(name, props: {...}) | A custom event with properties |
Stancelens.screen(name) | A screen view: a pageview with path /<name> |
Stancelens.identify(userId, traits: {...}) | Sets the user for later events and records $identify with the traits |
Stancelens.revenue(9.99, currency: 'EUR') | A purchase event with revenue. Pass event: for another name |
Stancelens.flag(key) / isEnabled(key) / flags() | Feature flags. flag and isEnabled record one $exposure per value; flags records none |
Stancelens.captureError(error, stack) | Records a handled error as $error. Pass fatal: true only for crashes |
Stancelens.reset() | Call on log out: new anonymous ID and session, no user |
Stancelens.optOut() / optIn() | Stops recording and deletes the queue; remembered |
Stancelens.flush() | Sends queued events now |
Flags are fetched with the app's platform, app version, OS, device type and current screen, so targeting rules on those apply. They're cached until identify, reset or flags(reload: true).
Errors
With init(captureErrors: true) the SDK records uncaught errors as $error with fatal: true and sends them at once. It chains into FlutterError.onError (build, layout and paint errors) and PlatformDispatcher.onError (uncaught async errors); handlers set before init still run. Native crashes in Kotlin, Java, Swift or Objective-C code aren't captured.
On the web, Flutter doesn't pass uncaught async errors to PlatformDispatcher.onError, so catch them with a zone:
void main() {
runZonedGuarded(() async {
await Stancelens.init(site: 'SITE_ID', host: 'https://stancelens.com', captureErrors: true);
runApp(const App());
}, (error, stack) => Stancelens.captureError(error, stack, fatal: true));
}What is sent
| Field | Source |
|---|---|
platform, sdk | flutter, stancelens-flutter/1.0.0 |
os | Flutter's target platform: Android, iOS, macOS, Windows or Linux |
os_version | Detected on iOS, macOS, Windows and Linux; on Android and the web pass osVersion |
device_type, screen | From the screen size in logical pixels: on Android and iOS a shortest side of 600 or more is a tablet; on the web the window width decides; desktop OSes are desktop |
language | The device locale |
app_version, device_model, timezone | Only what you pass to init, for example from package_info_plus and device_info_plus |
The SDK has no device-info plugin dependencies, reads no advertising or hardware IDs, and the install ID is a random UUID stored with shared_preferences.
Delivery
- Events are saved to
shared_preferenceswhen recorded (localStorage on the web) and sent in batches of up to 100: when 20 are queued, every 10 seconds, onflush(), and when the app is hidden or goes to the background. - Failed sends are retried with backoff from 1 second up to 5 minutes. Every event carries an ID, so the server drops a repeat.
- On the web, a send the browser cancels when the tab closes leaves the events stored; they go out on the next visit.
- Sessions end after 30 minutes without events, including time in the background (
sessionTimeout).
Testing your app
Pass store: MemoryStore() and a fake http.Client (such as MockClient from package:http/testing.dart) to init, and call Stancelens.close() in tearDown.