rollbar-flutter
rollbar-flutter copied to clipboard
RollbarFlutter.run is limiting (and probably not working as intended)
The current design of RollbarFlutter makes it impossible to integrate it with custom error handlers. Especially FlutterRollbar.run
is very limiting. IMHO it has several issues:
-
RollbarFlutter
overwritesFlutterError.onError
without any warnings. Having a customonError
function is not compatible withRollbarFlutter
. - There is no way to just initialize Rollbar without running an app. This basically makes
RollbarFlutter
incompatible with any other error handler. -
RollbarFlutter
is creating it's own guarded zone without the possibility to bubble up errors.FlutterRollbar
swallows all the errors without giving other error handlers a chance to catch an error. -
WidgetsFlutterBinding.ensureInitialized()
is called in the wrong place. When initializing a guarded zoneWidgetsFlutterBinding.ensureInitialized()
must be called within that zone. Calling it outside (as it is done inrun
) causes the guarded zone to not catch errors. At this point I think catching async errors does not properly work. Said that, with the current design one has to ensureWidgetsFlutterBinding.ensureInitialized()
is not called manually before runningRollbarFlutter.run
.
In my eyes the design of RollbarFlutter.run
is flawed. While the intention to make it as easy to integrate as possible is good, but it also bears the downside of inflexibility. Instead RollbarFlutter
should expose an API to initialize Rollbar and let the user decide how to implement. Something like this could work for example:
void main() async {
runZonedGuarded(() async {
// let the user be responsible for the guarded zone to
// properly run WidgetsFlutterBinding.ensureInitialized()
WidgetsFlutterBinding.ensureInitialized();
// Initialize rollbar
await RollbarFlutter.initialize();
// Give the users a chance to implement their own onError
FlutterError.onError = RollbarFlutter.onError;
runApp(MyApp());
}, (exception, stackTrace) {
// Also give users a chance to do anything else with async errors
RollbarFlutter.error(exception, stackTrace));
}
}
For convenience one could provide simple helper methods in RollbarFlutter
to make this an one-liner again. I'm not sure whether this is needed to be honest.
Please let me know what you think. I'd offer my support here to come up with an improved version. We are currently forced to work with a fork which I'd get rid of better sooner than later.