如何在riverpod中为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);

最新更新