lerix_flutter 0.3.0
lerix_flutter: ^0.3.0 copied to clipboard
The official Lerix Flutter SDK: error and crash reporting plus push notifications for Flutter apps on Android, iOS, macOS, Windows and Web.
example/lib/main.dart
import 'package:lerix_flutter/lerix_flutter.dart';
import 'package:flutter/material.dart';
void main() {
Lerix.init(
url: "https://api.lerix.dev/v1/YOUR_PROJECT_ID",
apiKey: "YOUR_API_KEY",
projectId: "YOUR_PROJECT_ID",
debugMode: true,
builder: () async {
WidgetsFlutterBinding.ensureInitialized();
runApp(const MyApp());
},
);
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String? _userId;
String? _deviceId;
String? _deviceToken;
@override
void initState() {
super.initState();
_checkUserRegistration();
}
Future<void> _checkUserRegistration() async {
await Lerix.notifications.init();
final deviceId = await Lerix.notifications.getDeviceId();
final deviceToken = await Lerix.notifications.getDeviceToken();
final granted = await Lerix.notifications.requestPermissions();
final subscribed =
await Lerix.notifications.subscribeToTopic('promotions');
print(subscribed);
setState(() {
_userId = Lerix.getUserId();
_deviceId = deviceId;
_deviceToken = deviceToken;
});
debugPrint("User ID: $_userId");
debugPrint("Permissions granted: $granted");
// This is what you pass in `deviceTokens` when sending a notification
// via the dashboard or REST API — NOT the raw platform token below.
debugPrint("Device id (use this to send notifications): $_deviceId");
debugPrint("Device token (raw APNs/FCM token): $_deviceToken");
}
deleteuser() async {
final us = await Lerix.deleteUser();
ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(content: Text("User Deleted")));
}
final String title = "Lerix Example";
@override
Widget build(BuildContext context) {
Lerix.notifications.setOnNotificationReceived((data) {
debugPrint("Notification Received: ${data.metadata}");
});
Lerix.notifications.setOnNotificationTapped((data) {
// debugPrint("Notification tapped: $data");
debugPrint("Notification tapped: ${data.metadata}");
});
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text(title),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Card(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'User Registration Status',
style: Theme.of(context).textTheme.titleLarge,
),
const SizedBox(height: 8),
Text('Registered: ${_deviceId != null ? "Yes" : "No"}'),
const SizedBox(height: 4),
Text('User ID: ${_userId ?? "Not available"}'),
const SizedBox(height: 4),
Text('Device ID: ${_deviceId ?? "Not available"}'),
],
),
),
),
const SizedBox(height: 20),
ElevatedButton(
onPressed: () {
debugPrint("User ID: ${Lerix.getUserId()}");
},
child: const Text("Print User ID to Console"),
),
const SizedBox(height: 12),
ElevatedButton(
onPressed: () async {
await _checkUserRegistration();
},
child: const Text("Check Registration Status"),
),
const SizedBox(height: 12),
ElevatedButton(
onPressed: () async {
await deleteuser();
},
child: const Text("Delete user"),
),
const SizedBox(height: 12),
ElevatedButton(
onPressed: () {
try {
throw Exception("Test exception");
} catch (exception, stack) {
Lerix.throwError(
exception,
stack,
bugSeverity: BugSeverity.high,
bugType: BugType.crash,
);
}
},
child: const Text("Throw Test Error"),
),
const SizedBox(height: 12),
ElevatedButton(
onPressed: () {
print("HERE");
try {
throw Exception("Test exception with metadata");
} catch (exception, stack) {
print("HERE2");
Lerix.throwError(
"WQEWQEQW",
stack,
bugSeverity: BugSeverity.high,
bugType: BugType.crash,
metaData: {
"name": "Test User",
"email": "test@example.com"
},
);
}
},
child: const Text("Throw Error With Metadata"),
),
],
),
),
),
);
}
}