MultiProvider in main instead of inside the tree

196

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.

**

Share:
196
Anonymous
Author by

Anonymous

Updated on January 04, 2023

Comments

  • Anonymous
    Anonymous over 1 year

    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?

  • Anonymous
    Anonymous about 2 years
    I understand that, my question was more about whether it is better practice to use all the providers in the MultiProvider before the material app or to nest them in the widgets
  • NiiLx
    NiiLx about 2 years
    In big applications, It is better practice to use all the providers in the MultiProvider before the material app.