Ad
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?
Ad
Answer
If you just need to rebuild the screen, calling setState()
should trigger a rebuild.
BlocBuilder
on the other hand rebuilds on state changes. If there's a state change that you'd like to observe in your bloc, calling something like context.read<ForumBloc>().doSomething()
should update the widgets inside BlocBuilder
.
Using BlocListener is another approach that you can use.
Ad
source: stackoverflow.com
Related Questions
- → How do you create a 12 or 24 mnemonics code for multiple cryptocurrencies (ETH, BTC and so on..)
- → Flutter: input text field don't work properly in a simple example..... where am I wrong?
- → Can I customize the code formatting of Dart code in Atom?
- → Is it possible to develop iOS apps with Flutter on a Linux virtual machine?
- → Display SnackBar in Flutter
- → JSON ObjectMapper in Flutter
- → Material flutter app source code
- → TabBarSelection No such method error
- → How do I set the animation color of a LinearProgressIndicator?
- → Add different routes/screens to Flutter app
- → Is there a way to get the size of an existing widget?
- → How to share a file using flutter
- → Is there an easy way to find particular text built from RichText in a Flutter test?
Ad