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

Issue

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);
  }

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(() {
          ...
        });

Answered By – Simon Danninger

Answer Checked By – Timothy Miller (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *