mobiqo_flutter 1.0.0
mobiqo_flutter: ^1.0.0 copied to clipboard
Mobiqo SDK for Flutter. Integrate with Mobiqo for user analytics, predictions, and insights.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:mobiqo_flutter/mobiqo_flutter.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// Initialize Mobiqo with your API key
// Replace 'YOUR_MOBIQO_API_KEY' with your actual API key
final mobiqo = Mobiqo();
await mobiqo.init(mobiqoKey: 'YOUR_MOBIQO_API_KEY');
runApp(MyApp(mobiqo: mobiqo));
}
class MyApp extends StatelessWidget {
final Mobiqo mobiqo;
const MyApp({super.key, required this.mobiqo});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Mobiqo Flutter SDK Example',
theme: ThemeData(
primarySwatch: Colors.blue,
useMaterial3: true,
),
home: MyHomePage(mobiqo: mobiqo),
);
}
}
class MyHomePage extends StatefulWidget {
final Mobiqo mobiqo;
const MyHomePage({super.key, required this.mobiqo});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String _status = 'Not initialized';
String _userInfo = '';
bool _isLoading = false;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: const Text('Mobiqo Flutter SDK Example'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Card(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'Status:',
style: TextStyle(fontWeight: FontWeight.bold),
),
const SizedBox(height: 8),
Text(_status),
],
),
),
),
const SizedBox(height: 16),
ElevatedButton(
onPressed: _isLoading ? null : _syncUser,
child: _isLoading
? const SizedBox(
width: 20,
height: 20,
child: CircularProgressIndicator(strokeWidth: 2),
)
: const Text('Sync User'),
),
const SizedBox(height: 8),
ElevatedButton(
onPressed: _isLoading ? null : _trackEvent,
child: const Text('Track Event'),
),
const SizedBox(height: 8),
ElevatedButton(
onPressed: _isLoading ? null : _getUserInfo,
child: const Text('Get User Info'),
),
const SizedBox(height: 16),
if (_userInfo.isNotEmpty)
Expanded(
child: Card(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'User Information:',
style: TextStyle(fontWeight: FontWeight.bold),
),
const SizedBox(height: 8),
Expanded(
child: SingleChildScrollView(
child: Text(_userInfo),
),
),
],
),
),
),
),
],
),
),
);
}
Future<void> _syncUser() async {
setState(() {
_isLoading = true;
_status = 'Syncing user...';
});
try {
final response = await widget.mobiqo.syncUser(
revenueCatUserId: 'example_user_123',
additionalData: {
'plan': 'premium',
'email': 'user@example.com',
'country': 'US',
},
);
if (response != null) {
setState(() {
_status = 'User synced successfully!';
_userInfo = '''
Username: ${response.appUser.mobiqoUsername}
OS: ${response.appUser.os} ${response.appUser.osVersion}
Country: ${response.appUser.country ?? 'Unknown'}
Language: ${response.appUser.language ?? 'Unknown'}
Purchase Intent: ${(response.statistics.purchaseIntent * 100).toStringAsFixed(1)}%
Is New User: ${response.isNewUser}
''';
});
} else {
setState(() {
_status = 'Failed to sync user';
});
}
} catch (e) {
setState(() {
_status = 'Error syncing user: $e';
});
} finally {
setState(() {
_isLoading = false;
});
}
}
Future<void> _trackEvent() async {
setState(() {
_isLoading = true;
_status = 'Tracking event...';
});
try {
await widget.mobiqo.trackEvent(
'button_pressed',
MobiqoEvent.click,
additionalData: {
'button_name': 'example_button',
'screen': 'main_screen',
},
);
setState(() {
_status = 'Event tracked successfully!';
});
} catch (e) {
setState(() {
_status = 'Error tracking event: $e';
});
} finally {
setState(() {
_isLoading = false;
});
}
}
Future<void> _getUserInfo() async {
setState(() {
_isLoading = true;
_status = 'Getting user info...';
});
try {
final userInfo = await widget.mobiqo.getUserInfo(
revenueCatUserId: 'example_user_123',
);
if (userInfo != null) {
setState(() {
_status = 'User info retrieved successfully!';
_userInfo = '''
Username: ${userInfo.appUser.mobiqoUsername}
OS: ${userInfo.appUser.os} ${userInfo.appUser.osVersion}
Country: ${userInfo.appUser.country ?? 'Unknown'}
Language: ${userInfo.appUser.language ?? 'Unknown'}
Purchase Intent: ${(userInfo.statistics.purchaseIntent * 100).toStringAsFixed(1)}%
Avg ARPU: \$${userInfo.statistics.avgArpu.toStringAsFixed(2)}
Avg LTV: \$${userInfo.statistics.avgLtv.toStringAsFixed(2)}
Active Entitlements: ${userInfo.appUser.activeEntitlements.join(', ')}
''';
});
} else {
setState(() {
_status = 'Failed to get user info';
});
}
} catch (e) {
setState(() {
_status = 'Error getting user info: $e';
});
} finally {
setState(() {
_isLoading = false;
});
}
}
@override
void dispose() {
widget.mobiqo.dispose();
super.dispose();
}
}