skip to Main Content

I need to access two different view model in one page in Flutter.
How to use nested Consumers in Flutter, Will they effect each other?

How can I use it in this code for example?

class CounterDisplay extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Consumer<CounterModel>(
      builder: (context, counterModel, child) {
        return Text('${counterModel.count}');
      },
    );
  }
}

2

Answers


  1. you can use Consumer2<> to access two different providers like this :

    class CounterDisplay extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Consumer2<CounterModel, SecondModel>(
          builder: (context, counterModel, secondModel, child) {
            return Text('${counterModel.count}');
          },
        );
      }
    }
    

    With this, your Text() widget will be rebuilt each time one the providers value is changed with notifyListener().

    If your Text() widget doesn’t need to be rebuilt with one of your providers, you can simply use Provider.of<MySecondProvider>(context, listen: false);.

    Here for example:

    class CounterDisplay extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Consumer<CounterModel>(
          builder: (context, counterModel, child) {
            MyThemeProvider myThemeProvider = Provider.of<MyThemeProvider>(context, listen: false);
            return Text('${counterModel.count}', color: myThemeProvider.isDark ? Colors.white : Colors.dark);
          },
        );
      }
    }
    
    

    I hope this helps!

    Login or Signup to reply.
  2. You can absolutely nest consumers, but you need to understand if you really want them to nest.

    Case 1: Nesting consumers:

    class CounterDisplay extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Consumer<CounterModel>(
          builder: (context, counterModel, child) {
            return Consumer<SomeAnotherModel>(
          builder: (anothercontext,anotherCounterModel, anotherChild) {
            return Text('${counterModel.count}');
          });
          },
        );
      }
    }
    

    Please note that if consumer for CounterModel is rebuilt, everything will be rebuilt. If consumer for SomeAnotherModel is rebuild, only the part inside its builder would be rebuilt.

    Instead of using consumers this way, I’d recommend them to be used as in case 2 below.

    Case 2: Use consumers on the required components:

    class CounterDisplay extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Column(
            children:[
            Consumer<CounterModel>(
              builder: (context, counterModel, child) { return...}),
            Consumer<CounterModel>(
              builder: (anotherContext, anotherModel, anotherChild) { return...}),
            ]
        );
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search