Flutter BottomSheet setstate cannot refresh main page

1.6k Views Asked by At

the setstate in bottomsheet is not working even using statefulwidget... i want to refresh the display of parent after popping bottomsheet

should i call setstate in void instead? how to do this ?

ElevatedButton(
             ...
   onPressed: () {
                  BottomSheet(context, id, expiry);
                  },
             ...
),


void BottomSheet(BuildContext context, String id, String expiry) {
  showModalBottomSheet(
    context: context,
    isScrollControlled: true,
    ....
        ),
      ),
      child: BottomSheetWidget(id, expiry),
    ),
  );
}


class BottomSheetWidget extends StatefulWidget {
  ....
}

class _BottomSheetWidgetState extends State<BottomSheetWidget> {
  ..
                onTap: () {
                  setState(() {
                    updated parent data here.....
                  });
                  Navigator.pop(context);
                  },
                ....
}
2

There are 2 best solutions below

0
On

Try using MaterialPageRoute to rebuild the display of the parent page

onTap: () {
  setState(() {
     updated parent data here.....
   });
  Navigator.pushReplacement(
    context,
    MaterialPageRoute(builder: (context) => ParentScreen()),
  );
}
0
On

Call .then method at the end of showModelBottomSheet()

 showModalBottomSheet(context: context, builder: builder).then((value) {
  setState(() {
    
  });
});