Skip to content

Commit 71d4e00

Browse files
committed
Fix DoNothingAction assertion when overriding Action.overridable
DoNothingAction (Action<Intent>) cannot pass the `is Action<T>` type check in _castAction when T is a concrete Intent subclass, because Dart reified generics are invariant. This causes an assertion failure when a DoNothingAction is mapped to a specific text editing intent (e.g. SelectAllTextIntent) via the Actions widget. The fix wraps Action<Intent> actions in _UnsafeCastAction<T> — a thin delegate with the correct generic type parameter — when intentType is exactly Intent. This is safe because Action<Intent>.invoke accepts any Intent, so passing a T (which extends Intent) is valid. Fixes flutter#180435
1 parent 7d48de2 commit 71d4e00

2 files changed

Lines changed: 91 additions & 6 deletions

File tree

‎packages/flutter/lib/src/widgets/actions.dart‎

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -921,13 +921,21 @@ class Actions extends StatefulWidget {
921921
final Action<Intent>? mappedAction = actionsMarker.actions[intent?.runtimeType ?? T];
922922
if (mappedAction is Action<T>?) {
923923
return mappedAction;
924-
} else {
925-
assert(
926-
false,
927-
'$T cannot be handled by an Action of runtime type ${mappedAction.runtimeType}.',
928-
);
929-
return null;
930924
}
925+
// An Action<Intent> (e.g. DoNothingAction) can handle any Intent subtype
926+
// but Dart's invariant reified generics cause `Action<Intent>` to fail the
927+
// `is Action<T>` check when T is a concrete Intent subclass. Since the
928+
// action was explicitly mapped to this intent type, it is safe to return.
929+
if (mappedAction.intentType == Intent) {
930+
// The unchecked cast is safe: Action<Intent>.invoke accepts any Intent,
931+
// so passing a T (which extends Intent) is valid.
932+
return _UnsafeCastAction<T>(mappedAction);
933+
}
934+
assert(
935+
false,
936+
'$T cannot be handled by an Action of runtime type ${mappedAction.runtimeType}.',
937+
);
938+
return null;
931939
}
932940

933941
/// Returns the [ActionDispatcher] associated with the [Actions] widget that
@@ -1486,6 +1494,49 @@ class DoNothingAction extends Action<Intent> {
14861494
void invoke(Intent intent) {}
14871495
}
14881496

1497+
/// Wraps an [Action<Intent>] so it can be used where an [Action<T>] is
1498+
/// expected. This is needed because Dart's reified generics make
1499+
/// `Action<Intent>` fail the `is Action<T>` check even though `T extends
1500+
/// Intent`.
1501+
class _UnsafeCastAction<T extends Intent> extends Action<T> {
1502+
_UnsafeCastAction(this._delegate);
1503+
final Action<Intent> _delegate;
1504+
1505+
@override
1506+
void addActionListener(ActionListenerCallback listener) {
1507+
super.addActionListener(listener);
1508+
_delegate.addActionListener(listener);
1509+
}
1510+
1511+
@override
1512+
void removeActionListener(ActionListenerCallback listener) {
1513+
super.removeActionListener(listener);
1514+
_delegate.removeActionListener(listener);
1515+
}
1516+
1517+
@override
1518+
bool get isActionEnabled => _delegate.isActionEnabled;
1519+
1520+
@override
1521+
bool isEnabled(T intent) => _delegate.isEnabled(intent);
1522+
1523+
@override
1524+
bool consumesKey(T intent) => _delegate.consumesKey(intent);
1525+
1526+
@override
1527+
KeyEventResult toKeyEventResult(T intent, Object? invokeResult) =>
1528+
_delegate.toKeyEventResult(intent, invokeResult);
1529+
1530+
@override
1531+
Object? invoke(T intent) => _delegate.invoke(intent);
1532+
1533+
@override
1534+
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
1535+
super.debugFillProperties(properties);
1536+
properties.add(DiagnosticsProperty<Action<Intent>>('delegate', _delegate));
1537+
}
1538+
}
1539+
14891540
/// An [Intent] that activates the currently focused control.
14901541
///
14911542
/// This intent is bound by default to the [LogicalKeyboardKey.space] key on all

‎packages/flutter/test/widgets/actions_test.dart‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1881,6 +1881,40 @@ void main() {
18811881
expect(LogInvocationContextAction.invokeContext, invokingContext);
18821882
});
18831883
});
1884+
1885+
// Regression test for https://github.com/flutter/flutter/issues/180435
1886+
testWidgets('DoNothingAction can override Action.overridable intents', (
1887+
WidgetTester tester,
1888+
) async {
1889+
await tester.pumpWidget(
1890+
WidgetsApp(
1891+
color: const Color(0xFFFFFFFF),
1892+
builder: (BuildContext context, Widget? child) {
1893+
return Actions(
1894+
actions: <Type, Action<Intent>>{SelectAllTextIntent: DoNothingAction()},
1895+
child: EditableText(
1896+
controller: TextEditingController(),
1897+
focusNode: FocusNode(),
1898+
style: TextStyle(),
1899+
cursorColor: Color(0xFF000000),
1900+
backgroundCursorColor: Color(0xFF000000),
1901+
),
1902+
);
1903+
},
1904+
),
1905+
);
1906+
1907+
await tester.tap(find.byType(EditableText));
1908+
await tester.pump();
1909+
1910+
// Press Ctrl+A — should not throw an assertion error.
1911+
await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft);
1912+
await tester.sendKeyEvent(LogicalKeyboardKey.keyA);
1913+
await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft);
1914+
await tester.pump();
1915+
1916+
expect(tester.takeException(), isNull);
1917+
});
18841918
}
18851919

18861920
typedef PostInvokeCallback =

0 commit comments

Comments
 (0)