Widgets not updating when I do a setState() In showModalBottomSheet - FLUTTER

49 views Asked by At

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

There are 2 answers

0
Shahed Emon On

Use StatefulBuilder to wrap the widget you are calling inside the bottom sheet.

0
shatlyk-jr On

It seems like you are experiencing an issue with updating widgets inside a showModalBottomSheet when using setState(). 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 the build() 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 like Provider or Bloc.

If the widget you want to update is outside the scope of setState(), you can use a GlobalKey to force the rebuild of that specific widget. Assign a GlobalKey to the widget and use key.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

 onPressed: () {
    _isBottomSheetVisible = true;
    setState(() {

    });
 },

Correct

onPressed: () {
  setState(() {
    _isBottomSheetVisible = true;
  });
},