heap_flutter_bridge 0.1.3 copy "heap_flutter_bridge: ^0.1.3" to clipboard
heap_flutter_bridge: ^0.1.3 copied to clipboard

Heap manual track SDK for Flutter apps on iOS, Android, and macOS.

example/lib/main.dart

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 {
      await Heap().setLogLevel(HeapLogLevel.debug);
      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')),
          ],
        )),
      ),
    );
  }
}
10
likes
160
pub points
84%
popularity

Publisher

verified publisherheap.io

Heap manual track SDK for Flutter apps on iOS, Android, and macOS.

Homepage

Documentation

API reference

License

MIT (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on heap_flutter_bridge