jatayu_flutter 0.2.0
jatayu_flutter: ^0.2.0 copied to clipboard
Mobile analytics, attribution, observability, and remote configuration for the Jatayu platform.
example/lib/main.dart
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:jatayu_flutter/jatayu_flutter.dart';
void main() => runApp(const ExampleApp());
class ExampleApp extends StatelessWidget {
const ExampleApp({super.key});
@override
Widget build(BuildContext context) => MaterialApp(
title: 'Jatayu SDK',
theme: ThemeData(colorScheme: ColorScheme.fromSeed(seedColor: const Color(0xff9b83f4), brightness: Brightness.dark), useMaterial3: true),
home: const ExampleScreen(),
);
}
class ExampleScreen extends StatefulWidget {
const ExampleScreen({super.key});
@override
State<ExampleScreen> createState() => _ExampleScreenState();
}
class _ExampleScreenState extends State<ExampleScreen> {
final endpoint = TextEditingController(text: const String.fromEnvironment('JATAYU_ENDPOINT', defaultValue: 'http://localhost:5173'));
final keyInput = TextEditingController(text: const String.fromEnvironment('JATAYU_PUBLIC_KEY'));
final user = TextEditingController(text: 'example-user');
JatayuClient? client;
bool consent = false, busy = false;
String status = 'Not connected';
Future<void> perform(Future<void> Function() operation) async {
setState(() => busy = true);
try { await operation(); if (mounted) setState(() => status = 'Done · ${client?.pendingEventCount ?? 0} queued events'); }
catch (error) { if (mounted) setState(() => status = error.toString()); }
finally { if (mounted) setState(() => busy = false); }
}
@override
void dispose() {
endpoint.dispose(); keyInput.dispose(); user.dispose();
unawaited(client?.dispose() ?? Future.value()); super.dispose();
}
@override
Widget build(BuildContext context) => Scaffold(appBar: AppBar(title: const Text('Jatayu SDK')), body: Center(child: ConstrainedBox(constraints: const BoxConstraints(maxWidth: 520), child: ListView(padding: const EdgeInsets.all(24), children: [
TextField(controller: endpoint, decoration: const InputDecoration(labelText: 'Jatayu endpoint')),
const SizedBox(height: 12),
TextField(controller: keyInput, decoration: const InputDecoration(labelText: 'Public SDK key')),
SwitchListTile(contentPadding: EdgeInsets.zero, title: const Text('Analytics consent'), value: consent, onChanged: busy ? null : (value) { setState(() => consent = value); if (client != null) unawaited(perform(() => client!.setConsent(value))); }),
FilledButton(onPressed: busy || client != null ? null : () => perform(() async { client = await JatayuClient.create(JatayuOptions(endpoint: endpoint.text.trim(), writeKey: keyInput.text.trim(), consent: consent, allowInsecureLocalhost: true)); }), child: const Text('Connect')),
const SizedBox(height: 24), Text(status),
if (client?.installationId != null) SelectableText('Installation: ${client!.installationId}'),
const SizedBox(height: 20),
TextField(controller: user, decoration: const InputDecoration(labelText: 'User ID')),
OutlinedButton(onPressed: busy || client == null || !consent ? null : () => perform(() => client!.identify(user.text, properties: {'name': 'Example user'}, setOnce: {'signup_source': 'example'})), child: const Text('Identify user')),
OutlinedButton(onPressed: busy || client == null || !consent ? null : () => perform(() => client!.setGroup('organization', 'demo', properties: {'plan': 'pro'})), child: const Text('Join demo group')),
OutlinedButton(onPressed: busy || client == null || !consent ? null : () => perform(() async { await client!.track('product_view', {'product_id': 'premium'}); await client!.flush(); await client!.heartbeat(); }), child: const Text('Send product_view')),
OutlinedButton(onPressed: busy || client == null || !consent ? null : () => perform(() async { await client!.fetchConfig(); if (mounted) ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(client!.getString('welcome_message', fallback: 'No welcome_message configured')))); }), child: const Text('Fetch remote config')),
OutlinedButton(onPressed: busy || client == null || !consent ? null : () => perform(() => client!.resetIdentity()), child: const Text('Reset identity')),
]))));
}