如何在 riverpod 中为 flutter 创建和修改布尔状态提供程序?

How do you create and modify a boolean state provider in riverpod for flutter?

我正在尝试创建一个全局提供程序,它将作为提供程序系列的一部分保存一个布尔值

final PaginationProvider = StateProvider.family((ref, id) => true);

但是我尝试像与其他提供商一样设置值的尝试失败了

context.read(PaginationProvider(0)).state = false;

具体错误是

Unhandled Exception: setState() or markNeedsBuild() called during build.
This UncontrolledProviderScope widget cannot be marked as needing to build because the framework is already in the process of building widgets.  A widget can be marked as needing to be built during the build phase only if one of its ancestors is currently building. This exception is allowed because the framework builds parent widgets before children, which means a dirty descendant will always be built. Otherwise, the framework might not visit this widget during this build phase.
The widget on which setState() or markNeedsBuild() was called was:
  UncontrolledProviderScope
The widget which was currently being built when the offending call was made was:
  Builder

好吧,我什至不知道为什么,但这解决了问题。

留在这里以防其他人遇到此问题

    Future.delayed(
        Duration.zero,
            () => context
            .read(PaginationProvider(0))
            .state = false);