简体   繁体   English

在带有 riverpod 的 flutter 中,如何在导航堆栈中已有的页面上更新 state

[英]In flutter with riverpod, how to update state on a page already in the navigation stack

I have some flutter/riverpod/navigator 2.0 pages that follow this flow.我有一些遵循此流程的 flutter/riverpod/navigator 2.0 页面。

list of items page -> navigate to -> edit item page -> back button -> list of items page.项目列表页面 -> 导航至 -> 编辑项目页面 -> 后退按钮 -> 项目列表页面。

the issue is when one hits the back button, the list of items is not updated with the new content, since that page is cached in the navigation stack of pages.问题是当点击后退按钮时,项目列表不会用新内容更新,因为该页面缓存在页面的导航堆栈中。

Are there any suggestions on how to handle this case?对如何处理这种情况有什么建议吗?

Ok I just got it working.好的,我刚开始工作。 The very short version is use a Consumer nested inside a ProviderListener on your 1st form.非常简短的版本是在您的第一个表单上使用嵌套在 ProviderListener 中的 Consumer。 For me the 1st form shows a list of retrieved models.对我来说,第一种形式显示了检索到的模型列表。 On that list page I have...在那个列表页上我有...

  Widget build(BuildContext context) {
    return ProviderListener(
      onChange: (context, state) {
      // Below Model.save in provider sets state to
      // "Success(model)". This triggers here and we
      // List.update in provider to update fields in
      // the Model in the List. Then the Consumer
      // below triggers on "Data(model)" state and the
      // List refreshes and shows updated Model
      if (state is Success) {
          context.read(regionListNotifierProvider.notifier).update(state.model);
        }
      },
      provider: regionNotifierProvider,
      child: Consumer(builder: (context, watch, child) {
        final state = watch(regionListNotifierProvider);

On the List page, each Tile is clickable.在 List 页面上,每个 Tile 都是可点击的。 When you click it uses the Model provider to set state to the Model you just clicked.当您单击时,它使用 Model 提供程序将 state 设置为您刚刚单击的 Model。 Then it Naviatates to the form.然后它导航到表单。 The form reads state and see the info for the Model you clicked in the List.表格显示为 state 并查看您在列表中单击的 Model 的信息。

In my ModelForm (where you edit the Model fields), when you save that fires the save method in it's provider.在我的 ModelForm 中(您在其中编辑 Model 字段),当您保存它时,它会触发其提供程序中的保存方法。 In there, it fire the.update above then the ModelForm Navigator.pops back to the List page, then above Listener and Consumer trigger in sequence and the List shows the updated data.在那里,它触发上面的更新,然后 ModelForm Navigator.pops 返回到列表页面,然后上面的监听器和消费者依次触发,列表显示更新的数据。

NOTE: The ProviderListener is listening to the ModelForm provider, then the Consumer is listening to the List provider.注意: ProviderListener 正在监听 ModelForm 提供者,然后 Consumer 正在监听 List 提供者。 Follow?跟随? This is THE trick that finally worked!这是终于奏效技巧!

NOTE2: These are all Stateless widgets.注 2:这些都是无状态小部件。 Only need the states in the Riverpod providers.只需要 Riverpod 提供程序中的状态。 They do everything.他们什么都做。

NOTE3: These 2 providers talk back-n-forth.注 3:这 2 个提供者来回交谈。 List gives the clicked model to form, when form saves, the List page hears that and updates it list of Models列表将点击的 model 提供给表单,当表单保存时,列表页面会听到并更新它的模型列表

I'm happy to add more details if you need them:-)如果您需要,我很乐意添加更多详细信息:-)

Wouldn't this example help to solve it?这个例子不会帮助解决它吗? The problem of navigation is there reduced to manipulating an immutable collection.导航问题被简化为操纵不可变集合。

Strictly typed navigation is used.使用严格类型的导航。 You can use navigate([Home(), Books(), Book(id: bookId)]);您可以使用navigate([Home(), Books(), Book(id: bookId)]); instead of navigate('home/books/$bookId');而不是navigate('home/books/$bookId'); in your code.在你的代码中。

See riverpod_navigator_example参见riverpod_navigator_example

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM