How to pass value from one provider to another directly in flutter - flutter

I have 2 provider, general provider and blog provider. general provider is called on splash screen and here I normally call API that fetch most common data like blog, category, tags, notification and is stored in general provider. How can I use the same data and populate other provider from the 'general provider' itself ? reason to do so is save number of calls made on the start of the app.

Related

How to pass data to another bloc

I have a login block, if the login data is successful, I need the OTP verification request, I need the successful login data to request the verification block. how do i get the data in the verification block?? by implementing a clean architecture
Navigate with named routes and pass it as an argument, put it inside the widget constructor or wrap your MaterialApp with your Bloc to access the instance globally.
You could also work with singletons, service locators, etc. This may also help you https://pub.dev/packages/get_it

Can a cubit inside feature "A" pull from a repository inside feature "B"? Is this bad design?

I'm using a Cubit-repository-datasource design separated by feature.
Title mostly says it all, but to extrapolate: I have an authentication Cubit. It is under the /authentication feature. I have another feature, /posts , that needs an access token from the /authentication feature to send an API request. This token is provided to it from the widget tree (authentication cubit is provided from the top of the widget tree). When a method from /posts's cubit is called: getPosts(int numOfPosts, String accessToken), it takes an access token from the provided widget tree's authentication cubit and passes it to the posts cubit. From here, it sends the request down to the repository layer, then to the data layer.
HOWEVER, what happens if the token is invalid? What happens if it has been tampered with? What happens if it's null? Then, I'd like the token to first be refreshed, and then to repeat the same call for posts using the refreshed access token. However, that "refresh the access token" method is inside the authentication cubit, not the posts cubit. How then should I call it? Can I call the refreshAccessToken() method (inside the /authentication feature's repository or data layer) from inside the repository or data layer of the /posts feature?
Is this sort of "crossing-feature" bad?
Thanks!
I second the comment made by #FabriBertani. The bloc/cubit shouldn't know anything about how to get the posts. It should just do getPosts(int numberOfPosts).
That method should e.g. come from an interface (abstract class in Dart) which in turn is implemented in a different layer (e.g. data/repository/service layer).
That implementation can then in turn handle what is needed to fetch the data from your API. It could e.g. have an authentication service injected to it from where it gets the token.

Flutter: How to refresh all ViewModels when a common property of service that that they're accessing has changed ? (MVVM Architecture)

Let's assume that, header, footer, drawer, body widget have their own indiviual viewmodels and they are all accessing UserService and displays the user's name.
What if the user's name has changed? how to refresh all viewmodels?
I think what you're looking for is a state management solution :
Introduction to state management
List of state management approaches
I personally recommend Riverpod, Provider or BLoC.

Flutter navigation auth middleware

I have built an application that uses local auth, I want if a user does not use the software for a long time (whether web or mobile app) I can detect it and take it to the password login page.
I try to use RouteObserver and I write a custom observer but is not good for navigation, it good for log and track.
medium
I know it's possible to add conditions before all my Navigator.push(), but I'm looking for a way to add middleware to all my routes.
use onGenerateRoute named parameter of 'MaterialApp` Widget.

Storing session data in flutter across pages best practice

I have a Flutter app which holds a username and a token to communicate with a web service.
How can I manage the username and token efficiently using best practices? Currently I am writing them into a DB and select them each time I want to do a request.
I tried to use bloc provider flutter bloc with a BlocProvider. I have the states LoggedIn and LoggedOut and the events Login and Logout.
Furthermore, I had a look at
secure storage, but I can't get the data available throughout all pages.
Also, I am not using the firebase API.
Let me know if I should provide some code snippets.
I use SharedPreferences now. The API is very simple and I wrote a wrapper to get rid of the (Strings) keys. Furthermore, the wrapper holds the values for the current session. The advantage of that is, that I can address the values directly instead of needing to read them asynchronously.