Flutter: How to implement FlutterError.OnError correctly
This is was design for a test. I switched to wrapping logic in try/catch then running expect() on the "error message text" present concept. ex:
try {
throw new UnrecognizedTermException();
} catch (e) {
setState(() => _status = e.errMsg());
}
// then in my test
expect(find.text('Could not determine the type of item you scanned'), findsOneWidget);
I use the code below in production to log errors to a server.
main.dart:
import 'dart:async';
import 'package:flutter/material.dart';
import 'logging.dart';
void main() async {
FlutterError.onError = (FlutterErrorDetails details) async {
new ErrorLogger().logError(details);
};
runZoned<Future<void>>(() async {
// Your App Here
runApp(MainApp());
}, onError: (error, stackTrace) {
new ErrorLogger().log(error, stackTrace);
});
}
logging.dart:
class ErrorLogger {
void logError(FlutterErrorDetails details) async {
//FlutterError.dumpErrorToConsole(details);
_sendToServer(details.exceptionAsString(), details.stack.toString());
}
void log(Object data, StackTrace stackTrace) async {
// print(data);
// print(stackTrace);
_sendToServer(data.toString(), stackTrace.toString());
}
void _sendToServer(String a, String b) async {
// Implementation here
}
}