Skip to content

Commit a603a17

Browse files
authored
Update MaterialStatesController docs for calling setState in a listener (flutter#143453)
fixes [Calling `setState` in a `MaterialStatesController` listener and `MaterialStateController.update` causes Exception](flutter#138986) ### Description `MaterialStatesController` listener calls `setState` during build when `MaterialStatesController.update` listener calls `notifyListeners`. I tried fixing this issue by putting `notifyListeners` in a post-frame callback. However, this breaks existing customer tests (particularly super editor tests). A safer approach would be to document that the listener's `setState` call should be in a post-frame callback to delay it and not call this during the build phase triggered by the `MaterialStatesController.update` in the widgets such as InkWell or buttons.
1 parent 8129797 commit a603a17

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

packages/flutter/lib/src/material/material_state.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,14 @@ class MaterialStatePropertyAll<T> implements MaterialStateProperty<T> {
760760
/// [MaterialState.focused] to its controller. When the widget gains the
761761
/// or loses the focus it will [update] its controller's [value] and
762762
/// notify listeners of the change.
763+
///
764+
/// When calling `setState` in a [MaterialStatesController] listener, use the
765+
/// [SchedulerBinding.addPostFrameCallback] to delay the call to `setState` after
766+
/// the frame has been rendered. It's generally prudent to use the
767+
/// [SchedulerBinding.addPostFrameCallback] because some of the widgets that
768+
/// depend on [MaterialStatesController] may call [update] in their build method.
769+
/// In such cases, listener's that call `setState` - during the build phase - will cause
770+
/// an error.
763771
class MaterialStatesController extends ValueNotifier<Set<MaterialState>> {
764772
/// Creates a MaterialStatesController.
765773
MaterialStatesController([Set<MaterialState>? value]) : super(<MaterialState>{...?value});

0 commit comments

Comments
 (0)