简体   繁体   English

如何在 initState 中使用 Riverpod 提供程序

[英]How to use a Riverpod provider in initState

I use Riverpod as state management in my web app.我在我的 web 应用程序中使用 Riverpod 作为 state 管理。 I am trying to build an AppBar that scrolls automatically to certain parts of a ListView.我正在尝试构建一个自动滚动到 ListView 的某些部分的 AppBar。

I created a ScrollController as a provider for this purpose.为此,我创建了一个 ScrollController 作为提供者。

final scrollControllerProvider = StateProvider<ScrollController?>((ref) => ScrollController());

To scroll, I use .animateTo from the AppBar actions.要滚动,我使用 AppBar 操作中的.animateTo

ref.read(scrollControllerProvider)!.animateTo(
  0,
  duration: const Duration(milliseconds: 500),
  curve: Curves.easeInOut,
);

The scrolling works, but it throws an exception滚动有效,但会引发异常

The provided ScrollController is currently attached to more than one ScrollPosition.

I have read that I should be using a StatefulWidget.我读过我应该使用 StatefulWidget。 However, using a ConsumerStatefulWidget I can't create the ScrollController using Riverpod, because I need to initiate it in initState() and I can't access to a provider from it.但是,使用 ConsumerStatefulWidget 我无法使用 Riverpod 创建 ScrollController,因为我需要在initState()中启动它并且我无法从它访问提供程序。 Is possible to have these two elements together?有可能将这两个元素放在一起吗?

class MyClass extends ConsumerStatefulWidget {
  const MyClass({Key? key}) : super(key: key);

  @override
  ConsumerState<MyClass> createState() => _MyClassState();
}

class _MyClassState extends ConsumerState<MyClass> {
  
  late ScrollController _scrollController;
  
  @override
    void initState() {
      super.initState();
      _scrollController = ScrollController();
    }
  
  @override
  Widget build(BuildContext context) {
    final status = ref.watch(someProvider);
    return ListView(
      controller: _scrollController,
    );
  }
}

You have access to ref inside any function that resides in the ConsumerStatefulWidget .您可以访问位于ConsumerStatefulWidget中的任何 function 中的ref So, you can simply call your provider in initState() function and it should behave normally所以,你可以简单地在initState() function 中调用你的提供者,它应该表现正常

...
  class _MyClassState extends ConsumerState<MyClass> {

  late ScrollController _scrollController;
  
  @override
    void initState() {
      super.initState();
      _scrollController = ref.watch(scrollControllerProvider); // Doesn't throw error
    }
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Riverpod 的 statenotifier 处理表单? - How can I use riverpod's statenotifier for handling a form? 如何在小部件构建中使用 initState 来引用 Riverpod 的值? - How can I use initState in a widget build to reference the value of riverpod? 如何使用 2 个可重新排序的行? - 例外:“ScrollController 附加到多个滚动视图。” - How can I use 2 reorderble rows ? - Exception: "ScrollController attached to multiple scroll views." Flutter:如何将 DraggableScrollableActuator.reset 和 DraggableScrollableSheet 中的 ScrollController 用于嵌套列表? - Flutter: How can I use DraggableScrollableActuator.reset AND the ScrollController from DraggableScrollableSheet for a nested list? 如何使用 Riverpod Notifier 创建下拉按钮? - how can i create drowdownbutton with Riverpod Notifier? 我如何召回构建中的 futureprovider (riverpod) - How can I recall a futureprovider (riverpod) on build 如何让 scrollController.listener 监听变化? - How can I make the scrollController.listener listen to changes? ScrollController 如何检测滚动开始、停止和滚动? - ScrollController how can I detect Scroll start, stop and scrolling? 我如何使用 StateNotifier Riverpod 来跟踪枚举值的变化 - How do i use StateNotifier riverpod to track the changes of enum value 如何使用 Riverpod 减少复制的代码 - How can I reduce the copied code using Riverpod
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM