Home > Back-end >  Flutter - Implement an async function from showBottomSheet
Flutter - Implement an async function from showBottomSheet

Time:09-21

I have a function that call showModalBottomSheet which I need to remove the barrier.

  Future<..> popup() async => await showModalBottomSheet<..>(
    ...
      builder : (BuildContext context){
        ...
        Navigator.of(context).pop(..);
        ...
      }
    );

I have two options. Since I don't want to use different Scaffold, that leaves me to use showBottomSheet instead. But I also want to keep my function as async because I need the BottomSheet to return a value on it's dismiss. showBottomSheet returns PersistentBottomSheetController not a Future. I have no idea how to implement a Future returning function from it, the way I did with showModalBottomSheet.

 Future<..> popup() async{
   ???
   Scaffold.of(context).showBottomSheet(..)<..>(
     (BuildContext context){
       ...
       Navigator.of(context).pop(..);
       ...
     }
   );
   ???
 }

Greatly appreciate the help. Thank you.

CodePudding user response:

You can't return Future from showModalBottomSheet instead you can use isClosed to check if bottom sheet closed and this getter return future.

you can use it like:

Future<bool> popup(BuildContext context) async{
    PersistentBottomSheetController myModal = showBottomSheet(
      context: context,
      builder: (context) {
        return const Text("Modal");
      },
    );
    Future<bool> isClosed = await myModal.closed;
    return isClosed;
  }
  • Related