Flutter MultiBlocProvider Performance

Issue

I have a MultiBlocProvider assigned for an app that has a Bottom Navigation Bar to navigate through main routes like Home, Search, Wishlist

I use setState(){} to change the currentPage for each route.

Recently I’ve added Blocs to each of them by using flutter_bloc package and I’m using BlocProvider to provide the bloc to each BlocBuilder,

 @override
  Widget build(BuildContext context) {
    return SafeArea(
      top: false,
      child: Scaffold(
        key: _scaffoldKey,
        body: PageStorage(
          child: Stack(
            children: <Widget>[
              AnimatedSwitcher(
                duration: Duration(milliseconds: 200),
                  child: BlocProvider<WishlistBloc>(
                      create: (BuildContext context) => WishlistBloc(WishlistRepository()),
                      child: currentPage),
              ),
              bottomBar(currentPageScroll)
            ],
          ),
          bucket: bucket,
        ),
      ),
    );
  }

Is it ok to use MultiBlocProvider to provide all the BlocsProviders I need?

they could be more than 10 providers, would it affect the performance of the app?

Solution

It’s definitely OK, MultiBlocProvider created for this purposes. But you need to understand, that if you with your creation also send(for e.x.) initialize event which started loading in all 10 blocs some data you will have some issues. So, if you will have some performance issues, please create separate SO question and community will help to find root-cause of this.

Answered By – Yauhen Sampir

Answer Checked By – Marie Seifert (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.