flutter_native_crash_log 0.0.3
flutter_native_crash_log: ^0.0.3 copied to clipboard
A Flutter plugin to capture native Android crashes, log non-fatal errors, and share crash logs as JSON.
import 'dart:convert';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_native_crash_log/flutter_native_crash_log.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: HomeScreen(),
);
}
}
class HomeScreen extends StatefulWidget {
const HomeScreen({super.key});
@override
State<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
@override
void initState() {
super.initState();
// Initialize the crash logger after the first frame
WidgetsBinding.instance.addPostFrameCallback((_) {
FlutterNativeCrashLog.initialize(context, trackLog: true, trackApi: true);
// Trigger the Beeceptor API call automatically after 2 seconds
Future.delayed(const Duration(seconds: 2), () async {
final client = HttpClient();
try {
final request = await client.getUrl(
Uri.parse('https://fake-json-api.mock.beeceptor.com/users'));
final response = await request.close();
await response.transform(utf8.decoder).join();
print("Auto-capture Beeceptor API triggered.");
} catch (e) {
print("Auto-capture failed: $e");
} finally {
client.close();
}
});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Native Crash Log Example'),
),
body: Center(
child: SingleChildScrollView(
padding: const EdgeInsets.all(20.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text('Tap the button below to force a native crash.'),
const SizedBox(height: 10),
ElevatedButton(
onPressed: () {
FlutterNativeCrashLog.forceCrash();
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.red,
foregroundColor: Colors.white,
),
child: const Text('FORCE BASIC NATIVE CRASH'),
),
const SizedBox(height: 20),
ElevatedButton(
onPressed: () {
FlutterNativeCrashLog.logNonFatalError(
'This is a non-fatal error');
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.orange,
foregroundColor: Colors.white,
),
child: const Text('LOG NON-FATAL ERROR'),
),
const SizedBox(height: 20),
ElevatedButton(
onPressed: () {
print("This is a standard Dart print statement.");
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
foregroundColor: Colors.white,
),
child: const Text('PRINT STANDARD LOG'),
),
const SizedBox(height: 20),
ElevatedButton(
onPressed: () {
throw FlutterError("This is a thrown Flutter Error.");
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.purple,
foregroundColor: Colors.white,
),
child: const Text('THROW FLUTTER ERROR'),
),
const SizedBox(height: 30),
const Divider(),
const Padding(
padding: EdgeInsets.symmetric(vertical: 20.0),
child: Text(
'AUTOMATIC API LOGGING DEMO\n(Captured behind the scenes)',
textAlign: TextAlign.center,
style: TextStyle(fontWeight: FontWeight.bold),
),
),
ElevatedButton(
onPressed: () async {
final client = HttpClient();
const url = 'https://fake-json-api.mock.beeceptor.com/users';
try {
final request = await client.getUrl(Uri.parse(url));
final response = await request.close();
await response.transform(utf8.decoder).join();
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('API call captured!')),
);
}
} catch (e) {
print("Request failed: $e");
} finally {
client.close();
}
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.green,
foregroundColor: Colors.white,
),
child: const Text('FETCH BEECEPTOR USERS'),
),
const SizedBox(height: 40),
const Text('After crashing, re-open the app to see logs.'),
const Text('Tap the floating bug icon to view logs.'),
],
),
),
),
);
}
}