Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it okay to supply all the providers on the root widget

Tags:

flutter

Let's say I have 6 providers. All of which are used in different spots in the app. Is it advisable/logical to make my root widget a MultiProvider whose child is the MaterialWidget and supply all the providers there. Then I can just use them wherever needed without worrying about whether or not they are above that particular widget.

Now I've tried and it works (obvsly). My question is, is it a good practice in terms of performance, code quality, etc?

@override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(
          create: (ctx) => Authentication(),
        ),
        ChangeNotifierProvider(
          create: (ctx) => Books(),
        ),
        ChangeNotifierProvider(
          create: (ctx) => HomeProvider(),
        ),
        ChangeNotifierProvider(
          create: (ctx) => Renk(),
        ),
      ],
      child: MaterialApp(

1 Answers

is it a good practice in terms of performance, code quality, etc?

generally according to your snippet i can't see any reason causes the application to perform poorly, but you can't guarantee that the tree will survive and that's a little terrifying when it comes to routes and it's consequent missteps in leaving some of it's treasures behind. or maybe others glitches that will not relate to routes , such as building a dirty tree , lets say that if one of these scenarios occurred, it would lead to catastrophic results and failure to respond (Unless you are completely careful with every process regarding possible errors)

on other hand i completely understand that you want quick and comprehensive access to these providers

i deeply recommended you to use riverpod instead , you can work with it globally and you can access to it in everywhere you want in your app

after 2 years using the provider you use i end up with riverpod i am so happy with it's amazing quick access .

like image 87
Mohammed Hamdan Avatar answered Nov 21 '25 09:11

Mohammed Hamdan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!