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.
**
Author by
Anonymous
Updated on January 04, 2023Comments
-
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 about 2 yearsI 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 about 2 yearsIn big applications, It is better practice to use all the providers in the MultiProvider before the material app.