Flutter – How to rebuild Widget with Bloc?

I am new to Bloc pattern in flutter and I am trying to understand this code here, I have a widget as:

Widget forumList() {
    return BlocProvider<ForumBloc>(
      create: (context) => ForumBloc(ForumService())..add(GetAllForumPosts()),
      child: BlocBuilder<ForumBloc, ForumState>(
        builder: (context, state) {
          if (state is ForumLoading) {
            return Text('Loading');
          }
          if (state is ForumLoaded) {
            var posts = state.forumPosts;
            return ListView.separated(
              shrinkWrap: true,
              physics: NeverScrollableScrollPhysics(),
              padding: const EdgeInsets.symmetric(vertical: 8),
              itemCount: posts.length,
              itemBuilder: (context, index) {
                var post = posts[index];
                return ForumPostCard(
                  forumModel: post,
                  onPressed: () {
                    Navigator.pushNamed(context, Routes.forumDetails,
                        arguments: {'post': post});
                  },
                );
              },
              separatorBuilder: (context, index) => SizedBox(height: 5),
            );
          }
          return SizedBox();
        },
      ),
    );
  }

Now how can I rebuild this widget after clicking a button?



Read more here: https://stackoverflow.com/questions/67931304/flutter-how-to-rebuild-widget-with-bloc

Content Attribution

This content was originally published by Asim Bhadra at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: