Report Any unhandled exception/error/failure to backend service as [ stacktrace, Device information, Risk level of this error ]
First create class that's send error to server
class RemoteReporter extends IRemoteReporter {
@override
// any uncaught error will be on this object
Future<void> report(UnhandledError error) async {
// call super if you want print [error] on the console
super.monitor(error);
// Api call that's send error to backend should write there.
}
}
Second is creating class that's determine risk level of error
class RiskLevelDeterminer implements IRiskLevelDeterminer {
@override
RiskLevel determine(ErrorDto error) {
if (error.errorObject is UnExpectedFailure) {
return RiskLevel.high;
}
return RiskLevel.low;
}
}
Finally main function should be like this :-
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
// objects from class we created before
final remoteReporter = RemoteReporter();
final riskLevelDeterminer = RiskLevelDeterminer();
final errorCapture = ErrorCapture(remoteReporter, riskLevelDeterminer);
// should call init
await errorCapture.init();
runZonedGuarded(() {
WidgetsFlutterBinding.ensureInitialized();
// our first handler
FlutterError.onError = errorCapture.handleFlutterError;
runApp(MyApp());
// our second handler
}, errorCapture.handleAsyncDartError);
}