bugsnag / bugsnag-flutter

BugSnag crash reporting for Flutter apps
https://docs.bugsnag.com/platforms/flutter/
MIT License
11 stars 11 forks source link

StateError fix in _createEvent method in client.dart #187

Closed msisinni closed 1 year ago

msisinni commented 1 year ago

Goal

StateErrors were being thrown from calling .first on an empty list in the method Future<BugsnagEvent?> _createEvent(.

Design

buildID is nullable anyway. bugsnag probably expected it'd be null because it's null on the first element of error.stacktrace instead of from error.stacktrace being empty. If you all want to fix it a different way, that's fine. This PR can just help point out what's wrong. Also, if you don't want the import for 'package:collection/collection.dart';, you can change it something else like

final String? buildID;
if (error.stacktrace.isNotEmpty)  {
  buildID = error.stacktrace.first.codeIdentifier;
}

Changeset

I used a method from collection.dart that doesn't cause StateErrors when trying to get the first element of a List. Because that method can return null, I added ? before .codeIdentifier. Also I removed import 'dart:ui'; because it was unused.

Testing

I tested throwing deliberate errors in my app locally and saw that they were successfully reported to bugsnag. Then I switched my production app to using my forked copy of the bugsnag_flutter repo.

msisinni commented 1 year ago

Here's the associated issue I raised https://github.com/bugsnag/bugsnag-flutter/issues/186

msisinni commented 1 year ago

Also note that I'm hoping you all will figure out why error.stacktrace is empty in the first place and fix that on your own, assuming that it's really not supposed to be empty. If it's allowed to be empty, then you all should double check your library for places where you call things like .first and .firstWhere, since those will throw errors on empty lists.

johnkiely1 commented 1 year ago

HI @msisinni, Thanks for this. As mentioned on https://github.com/bugsnag/bugsnag-flutter/issues/186#issuecomment-1346213128 we're going to do some digging on our side on this. I'll leave the PR open for now awaiting the outcome of that investigation.

mclack commented 1 year ago

Fix released in BugSnag Flutter v2.5.0