Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unhandled Exception: type 'Future<dynamic>' is not a subtype of type 'PersistentBottomSheetController<dynamic>' in type cast

I am now converting my codes into null safety. So now I have done this changes to my codes. Initially the return type was void then I have change into Future? but yet I do get the the error on this Unhandled Exception: type 'Future<dynamic>' is not a subtype of type 'PersistentBottomSheetController<dynamic>' in type cast. What are the other changes I can do to fix it.

Future<dynamic>? _showBottomSheet(
      context, MapDashboardDetails details) async {
   
    //_showPersBottomSheetCallBack = null;
    showingBottomSheet = true;

    _controller = _scaffoldKey.currentState!
        .showBottomSheet((context) {
          return VBottomSheet(
            details: details,            
            onCloseTapped: () {
              Navigator.of(context).pop();
            },
          );
        })
        .closed
        .whenComplete(() {
          if (mounted) {
            showingBottomSheet = false;
            _showPersBottomSheetCallBack = _showBottomSheet;
          }
        }) as PersistentBottomSheetController;
    return null;
  }

Here is how I am calling it. Even this was before void and then I added the Future and async but yet not able to resolve it.

Future<dynamic> _onMarkerTapped(MapDashboardDetails details) async {
  
    if (showingBottomSheet) {
      Navigator.of(context).pop();
      showingBottomSheet = false;
    }   
    _showBottomSheet(context, details);
  }

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

_scaffoldKey.currentState!
        .showBottomSheet((context) {
        ...
        })
        .closed
        .whenComplete(() {
          ...
        });

this returns a Future and not a PersistentBottomSheetController.
Therefor your cast(= as …) tries to cast a Future<void> to something which it is not. So the solution is to just return the future that whenComplete return
:

return _scaffoldKey.currentState!
        .showBottomSheet((context) {
        ...
        })
        .closed
        .whenComplete(() {
          ...
        });
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading