flutter_debug_log 1.0.1
flutter_debug_log: ^1.0.1 copied to clipboard
A lightweight, persistent, and floating debug log console for Flutter apps. Log errors, API requests, and custom messages with zero config.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:flutter_debug_log/flutter_debug_log.dart';
import 'package:dio/dio.dart';
void main() {
FlutterDebugLog.init();
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Debug Log Example',
theme: ThemeData(primarySwatch: Colors.blue),
builder: FlutterDebugLog.builder,
home: const MyHomePage(title: 'Flutter Debug Log Example'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final Dio _dio = Dio();
@override
void initState() {
super.initState();
// Add the interceptor
_dio.interceptors.add(DebugLogInterceptor());
}
void _logInfo() {
DebugLogManager().log(
type: LogType.info,
message: 'This is an info log triggered at ${DateTime.now()}',
);
}
void _logError() {
try {
throw Exception('This is a simulated exception');
} catch (e, stack) {
DebugLogManager().log(
type: LogType.error,
message: 'Something went wrong',
data: e.toString(),
stackTrace: stack,
);
}
}
Future<void> _makeApiCall() async {
try {
await _dio.get('https://jsonplaceholder.typicode.com/todos/1');
} catch (e) {
// Interceptor handles logging, but we can catch here too
}
}
Future<void> _makeErrorApiCall() async {
try {
await _dio.get('https://jsonplaceholder.typicode.com/invalid-endpoint');
} catch (e) {
// Interceptor handles logging
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
actions: [
IconButton(
icon: const Icon(Icons.bug_report),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const DebugLogScreen()),
);
},
),
],
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'Push buttons to generate logs:',
style: TextStyle(fontSize: 16),
),
const SizedBox(height: 20),
ElevatedButton(
onPressed: () {
_logInfo();
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Info Log Added!'),
duration: Duration(milliseconds: 500),
),
);
},
child: const Text('Log Info'),
),
const SizedBox(height: 10),
ElevatedButton(
style: ElevatedButton.styleFrom(backgroundColor: Colors.red),
onPressed: () {
_logError();
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Error Log Added!'),
duration: Duration(milliseconds: 500),
),
);
},
child: const Text(
'Log Error',
style: TextStyle(color: Colors.white),
),
),
const SizedBox(height: 10),
ElevatedButton(
onPressed: () async {
await _makeApiCall();
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('API Call Made! Check Logs.'),
duration: Duration(milliseconds: 500),
),
);
}
},
child: const Text('Make API Call (Success)'),
),
const SizedBox(height: 10),
ElevatedButton(
style: ElevatedButton.styleFrom(backgroundColor: Colors.orange),
onPressed: () async {
await _makeErrorApiCall();
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Failed API Call Made! Check Logs.'),
duration: Duration(milliseconds: 500),
),
);
}
},
child: const Text('Make API Call (Failure)'),
),
],
),
),
);
}
}