# Flutter integration

The Flutter package supports Android and iOS with Dart `>=3.9.0 <4.0.0` and
Flutter `>=3.35.0`.

## Install

```sh
flutter pub add dozz_analytics_sdk:^0.1.1
```

## Initialize once

```dart
import 'package:dozz_analytics_sdk/dozz_analytics_sdk.dart';
import 'package:http/http.dart' as http;

final httpClient = http.Client();
final analytics = DozzAnalyticsSdk(
  config: DozzAnalyticsConfig(
    endpoint: Uri.parse('https://api.example.com/sdk/v1/events'),
    writeKey: const String.fromEnvironment('DOZZ_WRITE_KEY'),
  ),
  storage: SharedPreferencesDozzStorage(),
  httpClient: HttpDozzClient(httpClient),
);

await analytics.initialize(initialUrl: launchUrl);
final lifecycle = DozzAnalyticsLifecycle(analytics)..start();
```

`launchUrl` is the initial URL supplied by the application's existing deep-link
integration. Forward later links with:

```dart
await analytics.captureAttribution(deepLink.toString());
```

## Consent and tracking

```dart
await analytics.setConsent(DozzConsent.granted);
await analytics.trackScreen('ride_home', {'entry_kind': 'deep_link'});
await analytics.trackRide('requested', {
  'vehicle_class': 'standard',
  'estimated_fare_bucket': '10_15',
});
```

Storage and configuration errors propagate. Keep product actions independent of
analytics and report failures through the application's existing error pipeline.

## Dispose owned resources

```dart
lifecycle.dispose();
httpClient.close();
```

The lifecycle observer flushes when the application leaves the foreground and
stops the SDK when disposed. `SharedPreferencesDozzStorage` stores consent,
anonymous identifiers, attribution, and the queue through
`SharedPreferencesAsync`.

See [API reference](api-reference.md), [events and privacy](events-and-privacy.md),
and the runnable [`flutter-demo`](../../../../flutter-demo/README.md).
