nlogger 1.0.0
nlogger: ^1.0.0 copied to clipboard
Nethru logging SDK
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:flutter/services.dart' show rootBundle;
import 'package:nlogger/nlogger.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return FutureBuilder(
// Call the initialization function asynchronously so that there is no UX usability impact.
// You may want to use FutureBuilder for this.
future: initNLogger(context),
builder: (context, snapshot) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
});
}
}
Future<void> initNLogger(BuildContext context) async {
// The configure() function must be called once the first time the app is run.
// At this time, you can specify various settings required.
// ignore: use_build_context_synchronously
await NLogger.configure(context, await rootBundle.loadString('assets/nlogger.json'));
// If the user is logged in, you can pass that information to the logging SDK as follows.
await NLogger.setUserId('nethru');
// The information you always want to send together with each log can be delivered as follows.
await NLogger.setSessionAttrs({
'nth_p1': 'v1',
'nth_p2': 'v2'
});
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter(BuildContext context) async {
// When you want to send logs, you can call the event() function.
NLogger.event(
action: 'click/plus_button',
params: {
'count': '${_counter + 1}'
},
// You must pass BuildContext to include locale information in the log.
context: context
);
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headlineMedium,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () => _incrementCounter(context),
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
}