The Tahaan
The Tahaan

Reputation: 7664

Inter-dependencies between Flutter Providers

How does one "Provider" class access elements in another?

Eg the "ApiController" needs the "userID" and the "sessionToken" from the UserInfo Provider.

The UserInfo provider needs to load these from the Shared preferences, but if these items are not yet inthe SharedPreferences, eg before the user logs in, then it will be null and the UserInfo needs to provide that.

The UserInfo further needs the ApiController to get other information about the user from the Api, eg the Avatar and other properties.

Upvotes: 1

Views: 147

Answers (2)

Hairon Chaviano
Hairon Chaviano

Reputation: 443

That's my advice on how to handle this particular case:

You can create a Splash View this page will be before the Login and on that page, you need to handle if the data is saved in the SharedPreferences or not. So let's do the following: you need to create a User DAO in this class you will include 3 functions (saveUser(),getUser() and removeUser()), so in the API class when you logged response is success you'll call the saveUser() and save the user data into the SharedPreferences, in the getUser() you check if the values exist in the SharedPreferences and if exist you return the data and if not return a message. Later in the Splash bloc you call the values of getUser() and in the view, you check if the values exist and navigate to the next page or launch an error message, is a lot of code to write but believe me is the best way. I almost forgot to say the removeUser() is for the Logout and the function is very simple just remove the values from the SharedPreferences

Upvotes: 1

Rémi Rousselet
Rémi Rousselet

Reputation: 277677

That is voluntarily unsupported. Provider forces uni-directional dataflow for a better code quality.

A potential solution in your situation is to split ApiController in multiple pieces, like UserController et SomethingController.

Upvotes: 0

Related Questions