Search code examples
flutterprovider

MultiProvider in main instead of inside the tree


Does inserting all providers in main affect performance? Is it a better option to put individual providers in the middle of the tree where they are needed?


Solution

  • When injecting many values in big applications, Provider can rapidly become pretty nested:

    Provider<Something>(
      create: (_) => Something(),
      child: Provider<SomethingElse>(
        create: (_) => SomethingElse(),
        child: Provider<AnotherThing>(
          create: (_) => AnotherThing(),
          child: someWidget,
        ),
      ),
    ),
    

    To:

    MultiProvider(
      providers: [
        Provider<Something>(create: (_) => Something()),
        Provider<SomethingElse>(create: (_) => SomethingElse()),
        Provider<AnotherThing>(create: (_) => AnotherThing()),
      ],
      child: someWidget,
    )
    

    **

    The behavior of both examples is strictly the same. MultiProvider only changes the appearance of the code.

    **