logrocket_flutter 1.53.3
logrocket_flutter: ^1.53.3 copied to clipboard
LogRocket SDK Plugin for Flutter. LogRocket sessions provide a comprehensive understanding of how users engage with your app.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:go_router/go_router.dart';
import 'package:logrocket_flutter/logrocket_flutter.dart';
void main() {
// initialize LogRocket session for your app id with all default configurations
LogRocket.wrapAndInitialize(LogRocketWrapConfiguration(),
LogRocketInitConfiguration(appID: 'YOUR_APP_ID'), () => runApp(MyApp()));
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (context) => MyAppState(),
// Capture visual session replay
child: LogRocketWidget(
child: MaterialApp(
title: 'Example App',
home: MyHomePage(),
// automatically track Navigator Route changes
navigatorObservers: [LogRocketNavigatorObserver('logrocket.example')],
),
),
);
}
}
class MyAppState extends ChangeNotifier {
var name = 'world';
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
var appState = context.watch<MyAppState>();
return SafeArea(
child: Scaffold(
body: Column(
children: [
ElevatedButton(
onPressed: () {
// identify a user
LogRocket.identify(appState.name, {
'favoriteColor': 'purple',
});
// capture a log message
LogRocket.info('Hello ${appState.name}');
},
child: Text('Hello ${appState.name}'),
),
ElevatedButton(
onPressed: () {
// track a custom event
LogRocket.track(LogRocketCustomEventBuilder('purchase-item')
..putRevenue(5.99));
},
child: Text('Purchase'),
),
// redact element from session replay
LogRocketRedact(
child: Text('hidden in replay', textDirection: TextDirection.ltr),
)
],
),
),
);
}
}
// Example of manually tracking GoRouter navigation with LogRocket
// when using StatefulShellRoutes.
// See NavigationScaffold for the navigation tracking calls.
class GoRouterStatefulShellSample extends StatelessWidget {
GoRouterStatefulShellSample({super.key});
final GoRouter _router = GoRouter(
// Cannot track navigation by registering a top level observer.
// GoRouter does not expose StatefulShellRoute route changes to any
// top level observers. https://github.com/flutter/flutter/issues/112196
initialLocation: '/a',
routes: <RouteBase>[
StatefulShellRoute.indexedStack(
builder: (BuildContext context, GoRouterState state,
StatefulNavigationShell navigationShell) {
return NavigationScaffold(navigationShell: navigationShell);
},
branches: <StatefulShellBranch>[
// ...
// your StatefulShellBranches here
// ...
],
),
],
);
@override
Widget build(BuildContext context) {
// Capture visual session replay
return LogRocketWidget(
child: MaterialApp.router(
title: 'GoRouter StatefulShell Example',
routerConfig: _router,
),
);
}
}
// Example of manually tracking GoRouter navigation with LogRocket
class NavigationScaffold extends StatelessWidget {
const NavigationScaffold({
super.key,
required this.navigationShell,
});
final StatefulNavigationShell navigationShell;
@override
Widget build(BuildContext context) {
// Track navigation in LogRocket by tagging pages with the current GoRouter URL.
// LogRocket will deduplicate if the same URL is tagged multiple times in a row.
LogRocket.tagPage(GoRouterState.of(context).uri.toString());
return Scaffold(
body: navigationShell,
bottomNavigationBar: BottomNavigationBar(
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Section A'),
BottomNavigationBarItem(icon: Icon(Icons.work), label: 'Section B'),
],
currentIndex: navigationShell.currentIndex,
onTap: (int index) => navigationShell.goBranch(index),
),
);
}
}