代码之家  ›  专栏  ›  技术社区  ›  user8012596

未处理的异常:类型“Future<dynamic>”不是类型强制转换中类型“PersistentBottomSheetController<dynamic>”的子类型

  •  0
  • user8012596  · 技术社区  · 3 年前

    我现在正在将我的代码转换为空安全。所以现在我已经对我的代码做了这些更改。最初的返回类型是void,然后我改成了Future?但是我确实明白这个错误 Unhandled Exception: type 'Future<dynamic>' is not a subtype of type 'PersistentBottomSheetController<dynamic>' in type cast 。我还可以做些什么修改来修复它。

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

    这是我如何调用它的。即使这是在void之前,然后我添加了Future和async,但仍然无法解决它。

    Future<dynamic> _onMarkerTapped(MapDashboardDetails details) async {
      
        if (showingBottomSheet) {
          Navigator.of(context).pop();
          showingBottomSheet = false;
        }   
        _showBottomSheet(context, details);
      }
    
    1 回复  |  直到 3 年前
        1
  •  1
  •   Simon Danninger    3 年前
    _scaffoldKey.currentState!
            .showBottomSheet((context) {
            ...
            })
            .closed
            .whenComplete(() {
              ...
            });
    

    这将返回Future而不是PersistentBottomSheetController。 因此,你的演员阵容(=as…)试图塑造一个未来<无效>而事实并非如此。因此,解决方案是让未来 whenComplete 回来 :

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