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

Flutter Riverpod Todo应用删除项触发RangeError(index)问题求助

解决Riverpod Todo应用删除项时的索引越界问题

问题根源在于你通过itemIdProvider传递索引,再用索引从todosProvider中获取Todo。删除操作后列表长度缩短,但已存在的TodoItemView组件仍会持有旧索引去访问数组,直接触发索引越界错误。

以下是修正方案:

1. 修改TodoItemView,直接接收Todo对象

不再依赖索引获取数据,而是直接传入目标Todo:

class TodoItemView extends HookConsumerWidget{
  final Todo todo;
  const TodoItemView({Key? key, required this.todo}) : super(key: key);

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    print("building for todo: ${todo.description}");

    return ListTile(
      leading: Checkbox(
        value: todo.isCompleted,
        onChanged: (value) {
          ref.read(todosProvider.notifier).toggle(todo);
        },
      ),
      title: Text(todo.description),
      trailing: IconButton(
        icon: const Icon(Icons.delete),
        onPressed: () {
          ref.read(todosProvider.notifier).removeTodo(todo);
        },
      ),
    );
  }
}

2. 重构TodoListView,移除不必要的ProviderScope

直接传递Todo给子组件,并用Todo的唯一标识作为组件Key:

class TodoListView extends HookConsumerWidget{
  const TodoListView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    print("building list view ");
    final todos = ref.watch(todosProvider);
    
    return ListView.builder(
      physics: const NeverScrollableScrollPhysics(),
      shrinkWrap: true,
      itemCount: todos.length,
      itemBuilder: (context, index) {
        final todo = todos[index];
        print("building for index ${index}, todo: ${todo.description}");
        // 使用Todo的唯一ID作为Key,确保组件复用逻辑正确
        return TodoItemView(key: ValueKey(todo.id), todo: todo);
      },
    );
  }
}

关键说明

  • 直接传递Todo对象彻底规避了索引依赖,从根源解决越界问题;
  • 用ValueKey(todo.id)替代UniqueKey(),让Flutter能精准识别组件对应的Todo,提升列表更新效率;
  • 移除冗余的itemIdProvider,简化状态管理逻辑。

注意:如果你的Todo类没有id字段,建议添加唯一标识(如UUID或自增ID),确保ValueKey的唯一性。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 19:52:32