@@ -760,11 +760,25 @@ SyntheticWheelEvent createSyntheticWheelEvent({
760
760
}
761
761
762
762
extension SyntheticEventTypeHelpers on SyntheticEvent {
763
- // Use getProperty(this, 'type') since, although statically we may be dealing with a SyntheticEvent,
764
- // this could be a non-event JS object cast to SyntheticEvent with a null `type`.
765
- // This is unlikely, but is possible, and before the null safety migration this method
766
- // gracefully returned false instead of throwing.
767
- bool _checkEventType (List <String > types) => getProperty (this , 'type' ) != null && types.any ((t) => type.contains (t));
763
+ // Access `type` in a try-catch since, although statically we may be dealing with a SyntheticEvent,
764
+ // this could be an object with a `null` `type` which would cause a type error since `type` is non-nullable.
765
+ //
766
+ // Cases where this could occur:
767
+ // - non-event JS object cast to SyntheticEvent
768
+ // - a mock class that hasn't mocked `type`
769
+ //
770
+ // We could use `getProperty(this, 'type')` to handle the JS object case, but for mock classes it would bypass the
771
+ // `type` getter and not behave as expected.
772
+ bool _checkEventType (List <String > types) {
773
+ String ? type;
774
+ try {
775
+ // This is typed as null statically, but if it's not, it will probably throw (depending on the compiler).
776
+ type = this .type;
777
+ } catch (_) {}
778
+
779
+ return type != null && types.any (type.contains);
780
+ }
781
+
768
782
bool _hasProperty (String propertyName) => hasProperty (this , propertyName);
769
783
770
784
/// Uses Duck Typing to detect if the event instance is a [SyntheticClipboardEvent] .
0 commit comments