I worked on a flutter program, but I have a problem that I can’t solve,
In showModalBottomSheet the widgets do not update when I do a setState();
I’ve already seen similar problems with the BottomSheet widget, but I need showModalBottomSheet
I would like to point out to you that I have a DraggableScrollableSheet in my showModalBottomSheet and once I close and reopen the widgets are updating but I want the widgets to update instantly
Can you help me resolve it?
Thanks in advance
I tried to do the same principle as BottomSheet but it didn’t work
2
Answers
Use
StatefulBuilder
to wrap the widget you are calling inside the bottom sheet.It seems like you are experiencing an issue with updating widgets inside a
showModalBottomSheet
when usingsetState()
.To resolve this problem, you can try the following fixes:
Firstly, ensure that you are calling
setState()
at the correct location. Make sure it is placed where you want the widget to update, and not outside thebuild()
method.Secondly, check if the widget you want to update is a direct child of the widget wrapped in
setState()
. If it is nested deeper, you might need to pass the updated data down the widget tree using callback functions or state management methods likeProvider
orBloc
.If the widget you want to update is outside the scope of
setState()
, you can use aGlobalKey
to force the rebuild of that specific widget. Assign aGlobalKey
to the widget and usekey.currentState.setState(() {})
to trigger a rebuild.Lastly, ensure that the data you are updating inside
setState()
is being passed correctly to the widget that requires the update. Double-check if the data is being modified as expected.I have seen a lot used it like below, but i think it is not proper way if you need top rebuild ui directly:
Incorrect
Correct