You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter调用deleteSighting()时出现Provider<ProfileCubit>找不到错误求助

解决Flutter中调用deleteSighting时找不到Provider的问题

问题原因

错误核心是弹窗的上下文隔离机制:

  • showModalBottomSheet和showDialog会创建独立的Widget树分支,它们内部的context是全新生成的,不在你最初通过BlocProvider注入ProfileCubit的层级范围内。
  • 你在_onTapped开头获取activeAtlas时,用的是传入的原始上下文(来自_MySightingsList,属于ProfileCubit的Provider覆盖层级),所以能正常访问;但弹窗按钮的onPressed里使用的是弹窗builder提供的新context,自然找不到对应的Provider。

解决方案

最直接的修复方式是提前在原始上下文获取ProfileCubit实例,避免在弹窗的隔离上下文里读取:

void _onTapped(BuildContext context, Sighting item) {
  final Sighting sighting = item;
  // 提前在原始上下文获取cubit实例并保存
  final profileCubit = context.read<ProfileCubit>();
  final Atlas atlas = profileCubit.state.activeAtlas;
  
  showModalBottomSheet(
    // 保留原有弹窗配置
    <...>
    builder: (context) => ContextMenu(
      items: [
        ContextMenuItem(
          // 其他菜单配置
          <...>
        ),
        ContextMenuItem(
          // 删除菜单配置
          <...>
          onTap: () {
            showDialog(
              context: context,
              builder: (context) => AlertDialog(
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(16.0),
                ),
                <...>
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Text(
                      translate("activities.delete_popup.description"),
                    ),
                    const SizedBox(height: 24),
                    CustomElevatedButton(
                      onPressed: () async {
                        // 直接使用提前获取的cubit实例,无需依赖弹窗上下文
                        await profileCubit.deleteSighting(sighting, atlas);
                        // 后续关闭弹窗等逻辑
                        <...>
                      },
                    ),
                  ],
                ),
              ),
            );
          },
        ),
      ],
    ),
  );
}

补充说明

如果需要在弹窗内访问其他Provider,还可以通过以下方式处理:

  1. 在弹窗的builder中使用BlocProvider.value传递已有的cubit实例
  2. 使用GlobalKey获取原始上下文(不推荐,会增加代码复杂度)

但上述提前获取实例的方案是最简洁且不易出错的。

内容的提问来源于stack exchange,提问作者dreanor

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 12:04:51