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

Flutter:Navigation.pop时如何刷新父组件的BlocBuilder

Hey, let's fix this issue where your bookmarked list doesn't refresh after un-favoriting a show from the details page. Since you're using Bloc, we can leverage the navigation's then() callback to trigger a reload in your parent component's Bloc—even though the push() happens in the child. Here are a couple of straightforward approaches:

1. Trigger Bloc Reload Directly After Navigation Returns

In your child ListOfShows component, after the user returns from the details page, you can fetch the parent BookmarkShowsBloc via context and dispatch the LoadBookmarkedShows event again. This will force the Bloc to re-fetch the updated list and refresh the BlocBuilder.

Modify your showItem's onTap method like this:

InkWell showItem(Show show, BuildContext context) {
  return InkWell(
    onTap: () async {
      // Wait for the user to return from details page
      await Navigator.of(context).push(MaterialPageRoute(
        builder: (context) => showDetails(show)));
      
      // Get the parent Bloc and trigger reload
      final bookmarkBloc = BlocProvider.of<BookmarkShowsBloc>(context);
      bookmarkBloc.add(LoadBookmarkedShows());
    },
    child: Container(
      child: CachedNetworkImage(
        imageUrl: show.portraitPoster
      ),
    ),
  );
}

This works because the context in ListOfShows has access to the BlocProvider from your parent BookmarkedShows component—since the child is nested inside the BlocBuilder.

2. Conditional Reload Based on Details Page Action

If you only want to refresh when the user actually un-bookmarked the show (not every time they navigate back), you can pass a return value from the details page.

First, update the child's navigation to expect a return value:

onTap: () async {
  // Expect a boolean return value indicating if bookmark status changed
  final didModifyBookmark = await Navigator.of(context).push<bool>(MaterialPageRoute(
    builder: (context) => showDetails(show)));
  
  // Only reload if the bookmark was modified
  if (didModifyBookmark == true) {
    final bookmarkBloc = BlocProvider.of<BookmarkShowsBloc>(context);
    bookmarkBloc.add(LoadBookmarkedShows());
  }
}

Then, in your details page, when the user un-bookmarks the show, pass true when popping the navigator:

// Example un-bookmark button in details page
ElevatedButton(
  onPressed: () {
    // Handle un-bookmark logic here (update your data source, etc.)
    // ...
    
    // Pop back to the list, indicating the bookmark was modified
    Navigator.of(context).pop(true);
  },
  child: Text("Remove from Bookmarks"),
)

If the user navigates back without changing the bookmark (e.g., pressing the back button), didModifyBookmark will be null, so we skip the reload—this is more efficient!

A Quick Note About Your Parent Component

Just a heads-up: your BookmarkedShows is a StatelessWidget that creates the BookmarkShowsBloc inside the build method. If the widget rebuilds for any reason (like screen rotation), it will create a new Bloc instance. To avoid this, consider moving the Bloc creation higher up in the widget tree (e.g., in a StatefulWidget's initState or using a dependency injection setup) — but that's optional for solving your current refresh issue.

内容的提问来源于stack exchange,提问作者Santhosh Thamaraiselvan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 20:57:38