flutter_ai_debugger 0.1.2
flutter_ai_debugger: ^0.1.2 copied to clipboard
AI-powered Flutter error tracker & debugger using Gemini: capture, analyze, store (Hive), and export (CSV/JSON).
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_ai_debugger/flutter_ai_debugger.dart';
final GlobalKey<NavigatorState> appNavigatorKey = GlobalKey<NavigatorState>();
void main() {
runZonedGuarded(() async {
WidgetsFlutterBinding.ensureInitialized();
// Tip: store API key in .env or secure storage
const envKey = String.fromEnvironment('GEMINI_API_KEY');
const fallback = 'YOUR_GEMINI_API_KEY';
final apiKey = envKey.isEmpty ? fallback : envKey;
await AiDebugger.init(
AiDebugConfig(
apiKey: apiKey,
enableInternalLogs: true,
captureFlutterErrors: true,
capturePlatformDispatcherErrors: true,
captureZoneErrors: true,
modelName: "gemini-2.5-flash", // optional
),
onNewReport: (r) {
// Example: show a Toast/Log when a new report arrives
// debugPrint("New AI report: ${r.id}");
},
);
runApp(const MyApp());
}, (e, s) => AiDebugger.logError(e, s));
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
late DateTime mustInit;
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'flutter_ai_debugger Example',
navigatorKey: appNavigatorKey,
builder: (context, child) {
return Stack(
children: [
if (child != null) child,
AiDebugDraggableButton(
navigatorKey: appNavigatorKey,
),
],
);
},
home: Builder(builder: (context) {
return Scaffold(
appBar: AppBar(
title: const Text('AI Debugger Example'),
),
body: ListView(
padding: const EdgeInsets.all(16),
children: [
Builder(builder: (context) {
return ElevatedButton(
onPressed: () => Navigator.of(context).push(
MaterialPageRoute(builder: (_) => const AiDebugDashboard()),
),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.grey.shade800,
foregroundColor: Colors.white,
elevation: 0,
),
child: const Text(
'Open AI Debug Dashboard',
style: TextStyle(
color: Colors.red,
fontWeight: FontWeight.bold,
),
),
);
}),
// Errors playground
ElevatedButton(
onPressed: () => throw Exception('Forced error'),
child: const Text('Exception: Forced error'),
),
ElevatedButton(
onPressed: () {
final list = <int>[];
// ignore: avoid_print
print(list[10]);
},
child: const Text('RangeError: list[10]'),
),
ElevatedButton(
onPressed: () {
String? s;
// ignore: avoid_print
print(s!);
},
child: const Text('Null check operator on null'),
),
ElevatedButton(
onPressed: () {
const d = null as dynamic;
// ignore: avoid_dynamic_calls
d.foo();
},
child: const Text('NoSuchMethodError on null'),
),
ElevatedButton(
onPressed: () {
// ignore: avoid_print
print(int.parse('abc'));
},
child: const Text('FormatException: int.parse("abc")'),
),
ElevatedButton(
onPressed: () => throw StateError('Bad state example'),
child: const Text('StateError'),
),
ElevatedButton(
onPressed: () {
// ignore: avoid_print
print(mustInit);
},
child: const Text('LateInitializationError'),
),
ElevatedButton(
onPressed: () async {
await Future.delayed(const Duration(milliseconds: 10));
throw Exception('Async error from Future');
},
child: const Text('Async error (Future)'),
),
ElevatedButton(
onPressed: () {
Future.microtask(() {
throw Exception('Microtask thrown error');
});
},
child: const Text('Microtask error'),
),
ElevatedButton(
onPressed: () {
FlutterError.reportError(FlutterErrorDetails(
exception: Exception('Reported via FlutterError'),
stack: StackTrace.current,
library: 'test harness',
context: ErrorDescription('triggered by button'),
));
},
child: const Text('FlutterError.reportError'),
),
ElevatedButton(
onPressed: () async {
await AiDebugger.logError(
Exception('Manual logged error'),
StackTrace.current,
);
},
child: const Text('Manual log via AiDebugger'),
),
ElevatedButton(
onPressed: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (_) {
throw Exception('Error inside route builder');
},
),
);
},
child: const Text('Error in Route builder'),
),
const SizedBox(height: 20),
],
),
);
}),
);
}
}