'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 1:[1]

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.

**

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 NiiLx