Skip to content

Commit 5aab4c5

Browse files
authored
SentryUserInteractionWidget checks if the Elements are mounted before comparing them (#1339)
1 parent 1ab55fc commit 5aab4c5

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
- [changelog](https://github.com/getsentry/sentry-cocoa/blob/main/CHANGELOG.md#831)
1313
- [diff](https://github.com/getsentry/sentry-cocoa/compare/8.0.0...8.3.1)
1414

15+
### Fixes
16+
17+
- SentryUserInteractionWidget checks if the Elements are mounted before comparing them ([#1339](https://github.com/getsentry/sentry-dart/pull/1339))
18+
1519
## 7.0.0
1620

1721
### Features

flutter/lib/src/user_interaction/sentry_user_interaction_widget.dart

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,11 @@ class _SentryUserInteractionWidgetState
151151
);
152152

153153
final activeTransaction = _activeTransaction;
154+
final lastElement = _lastTappedWidget?.element;
154155
if (activeTransaction != null) {
155-
if (_lastTappedWidget?.element.widget == element.widget &&
156+
if (_isElementMounted(lastElement) &&
157+
_isElementMounted(element) &&
158+
lastElement?.widget == element.widget &&
156159
_lastTappedWidget?.eventType == tappedWidget.eventType &&
157160
!activeTransaction.finished) {
158161
// ignore: invalid_use_of_internal_member
@@ -339,4 +342,26 @@ class _SentryUserInteractionWidgetState
339342

340343
return null;
341344
}
345+
346+
bool _isElementMounted(Element? element) {
347+
if (element == null) {
348+
return false;
349+
}
350+
try {
351+
// ignore: return_of_invalid_type
352+
return (element as dynamic).mounted;
353+
} on NoSuchMethodError catch (_) {
354+
// mounted checks if the widget is not null.
355+
356+
try {
357+
// Flutter 3.0.0 does `_widget!` and if `_widget` is null it throws.
358+
359+
// ignore: unnecessary_null_comparison
360+
return element.widget != null;
361+
} catch (_) {
362+
// if it throws, the `_widget` is null and not mounted.
363+
return false;
364+
}
365+
}
366+
}
342367
}

0 commit comments

Comments
 (0)