I have a bottom nav bar that has a list page that uses a bloc for the state.
class _MainPageState extends State<MainPage> {
int _index = 0;
@override
Widget build(BuildContext context) {
final List<Widget> _widgets = [
const ListPage(),
Scaffold(),
Scaffold(),
];
return Scaffold(
body: IndexedStack(
index: _index,
children: _widgets,
),
bottomNavigationBar: BottomNavigationBar(
...
class ListPage extends StatelessWidget {
const ListPage({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (_) =>
getIt<ListBloc>()..add(const ListEvent.load(limit: 10)),
child: SafeArea(
child: Scaffold(
appBar: AppBar(),
body: const List(),
),
),
);
}
}
The problem is build
is beeing called 4 times. This causes the event fetching the list 4 times.
Don't know where to add the event to prevent re-builds.
If I add the event in statefull widget's initState
. Bloc does not recognize ListBloc beeing in the context when fetching the bloc down the widget tree.