Routing with Provider: An Experiment

Atri Das
2 min readJun 19, 2020

Hello folks, I hope you all are staying safe and healthy. Recently I was experimenting with the flutter provider package and I have tried to route between pages with the ChangeNotifierProvider.

There are so many pros and cons of this, but I will be sharing with you what I have learned during my experimental phase.

Well, let’s come to the point. Take a scenario where you have a bottom bar on a screen and there are some specific elements. By clicking those elements, you will get a different screen but the bottom bar will remain constant.

Step 1:

import the provider package on your pubspec.yaml

Step 2:

Let’s write the logic. At first, we will create an enum where we will write all the possible screens that we have.

Step 3:

Now we will write the class which will be extended to ChangeNotifer.

As we can see, the currentScreen variable is holding a value from the enum. When the function routeScreen will get a value, depending on that value, the currentScreen variable will store another value and send it to the UI.

Step 4:

We will wrap our MaterialApp with ChangeNotifierProvider and get an object of the newly created class and pass the value to the descendants.

Step 5:

Time to create the RootScreen.

Now we are simply fetching the currentScreen value. Depending on that value, screens are coming. When we tap on each icon, we are sending a string through the routeScreen function and changing the value of the currentScreen.

It is just an experimental project and I will be glad to know your views about the pros and cons of this project.

Here is the Github Repo of this project. Pull requests are welcome to make a better codebase.

--

--

Atri Das

Developer Student Clubs Alumni by Google Developers || Flutter Developer and Explorer