heap_flutter_bridge 0.1.0 heap_flutter_bridge: ^0.1.0 copied to clipboard
Heap manual track SDK for Flutter on native platforms.
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:heap_flutter_bridge/heap_flutter_bridge.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _message = 'Heap not initialized';
@override
void initState() {
super.initState();
initHeap();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initHeap() async {
try {
HeapLogger().setLogLevel(HeapLogLevel.debug);
HeapLogger().info("Logging something.");
await Heap().startRecording('1501760456');
await loadMessage();
} on PlatformException {
setMessage('Something bad happened initializing Heap.');
}
}
Future<void> loadMessage() async {
try {
String userId = await Heap().getUserId() ?? 'unknown';
String identity = await Heap().getIdentity() ?? '(unidentified)';
String sessionId = await Heap().fetchSessionId() ?? 'unknown';
setMessage(
"Heap initialized with:\nUserId: $userId\nIdentity: $identity\nSessionId: $sessionId");
} on PlatformException {
setMessage('Something bad happened in Heap.');
}
}
void setMessage(String message) {
if (!mounted) return;
setState(() {
_message = message;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('$_message\n'),
ElevatedButton(
onPressed: () {
Heap().identify('user-1');
loadMessage();
},
child: const Text('Identify')),
const SizedBox(height: 10),
ElevatedButton(
onPressed: () {
Heap().resetIdentity();
loadMessage();
},
child: const Text('Reset Identity')),
const SizedBox(height: 10),
ElevatedButton(
onPressed: () {
Heap().track('my-event', {
'my-field': 'my-value',
'custom': true,
});
loadMessage();
},
child: const Text('Track')),
],
)),
),
);
}
}