Flutter GetX Auth ever function - flutter

I am completely new to GetX and want to learn an easier state management system than bloc ( I started with BLoC). So the first thing I wanted to learn is how authentication works. I found this tutorial online: https://dev.to/imransefat/firebase-authentication-with-getx-in-flutter-4ik8 and asked me a question.
Inside the controller, the author bound two streams. One for the user and one for the googleSignInUser and he also added an ever function for googleSignIn:
googleSignInAccount.bindStream(googleSign.onCurrentUserChanged);
ever(googleSignInAccount, _setInitialScreenGoogle);
. When I user exactly this source-code and add a Firestore crud controller with a Todo-list, I get a null value on the variable auth.currentUser! on the Stream for the Todo list ( firebaseFirestore.collection('/users/${**auth.currentUser!**.uid}/FoodList').snapshots().map((QuerySnapshot query) {... )
When I remove this function inside the onReady method, everything works fine. If I understand this correctly, I don't have to listen to the googleSignIn stream specifically, it should be enough to listen to the user stream. Is this a mistake from him, or do I misunderstand here something? You can find the complete source code on the website.
Second question is: Before GetX I always check if a user is authenticated or not with a StreamBuilder inside the Material app:
StreamBuilder<User?>(
stream: auth.authStateChanges(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return const RootPage();
} else {
return TutorialRoot();
}
With GetX I saw this appoach with the _setInitialScreen(User? user) method and the
ever(firebaseUser, _setInitialScreen);
call inside the onReady(); - Is the StreamBuilder a bad practice when using GetX, or still a legit way of checking the current auth status? In my opinion the user expirince is a little bit faster with the StreamBuilder that why I ask for this.

Related

How do I add a widget to my web app in run time permanently?

I am coding an web app that allows the user to create a type of electronic document using widgets (that I then store in a list view). Is it possible to add this list view to my web app in run time so other users on other devices can access the electronic form?
The only way to do this would be to use some sort of database to hold the resulting "document". You could use Firebase Firestore, the server's file system, or anything else you can connect to. There is no way to easily modify the underlying code, as it is no longer Dart code; it gets transpiled to JavaScript.
Assuming you go with Firestore, you could have something like this:
CollectionReference documentRef = Firestore.instance
.collection('forms')
.document(formId);
documentRef.snapshots().listen((DocumentSnapshot documentSnapshot) {
// Do something with `documentSnapshot`.
// setState(() => _updateForm(documentSnapshot.data.data()));
});

How to prevent other widgets from staying active when leaving them in flutter

I have an app with many pages/interfaces and most of them are getting the data from an api, when i leave a page and go to another, the previous one stays active forever.
I noticed that when prenting the data result on every page and i noticed even when i leave the page before finishing it job, it keep fetching the data at the same time with the next page that i opened.
This caused me many problems when checking many pages one after another and made the app lag or close connection with the api.
I want to know how to prevent this from happenening
This is how i go from one page to another
Navigator.of(context).push(
MaterialPageRoute(builder: (BuildContext context)=>Dashboard()
));
You need to dispose the previous page. You can do it by pushReplacement route state. It will dispose all your previous page's methods which are currently running.
Navigator.of(context).pushReplacement(
MaterialPageRoute(builder: (BuildContext context)=>Dashboard()
));

Flutter reset all provider states after sign out

Whenever I sign out of my app, it seems to not clear all provider states and when I log in to another user my app crashes. Is there way to reset everything to default when performing logout action?
It depends on the scope of your provided objects. If you provide them at top level, the instances will not be disposed and live on.
Possible solutions could be:
Provide your state further down in the widget tree.
Observe a logout event in your state and perform reset of state automatically. There are multiple ways to do this, e.g. use a ProxyProvider.
ProxyProvider<Session, SomeStateClass>(
create: (_) => SomeStateClass(),
update: (_, session, instanceOfStateClass) {
return instanceOfStateClass..reinitialize(session);
}),
In the example above, the update method gets called, when Session notifies about changes.

how can i access to data from another bloc

hey guys i'm new with bloc pattern and i have some questions
How I can get props data from a State inside BlocBuilder for another bloc ?
already worked with provider (state management) and it's easy to access to any data from everywhere , is that possible with bloc and how ?
thank you
Hoping that I understood your question correctly,
You have a piece of data stored in state in bloc A and you want to use that data in bloc B.
For instance a post Bloc which needs to fetch all the posts by a logged in user (assuming that the logged in user has been stored in state in the user Bloc).
Defining the event:
class FetchPost extends PostEvent{
final User currentUser
}
when dispatching the FetchPost event:
Note: if you are dispatching the event from your view, you have to get the bloc from Provider.of(context)
PostBloc _postBloc = Provider.of<TransactionBloc>(context);
UserBloc _userBloc = Provider.of<UserBloc>(context);
_postBloc.dispatch(FetchPost(_userBloc.currentState.loggedInUser))
To access other blocs props you have to hold its instance, and from it, you can acquire its state. I have addressed this in the blog post that I have written.
thanks guy i think i find the solution of this question
ist's data shouldn't be in the bloc, data should be in a repository. Then, you can use RepositoryProvider or MultiRepositoryProvider in order to get any data from any bloc in the subtree.

Flutter cookbook for widget that gets its state from a REST API and refreshes automatically?

What is the basic cookbook for a flutter widget that does this?
1) Displays initial state like "loading".
2) Async gets content from a REST API.
3) Updates state when the REST API returns success.
4) Refreshes content on a timer.
You may want to take a look here.
Generally speaking, you will have a StatefulWidget that calls the API on it's initState and store the "future" (async action in Dart) locally.
Then use FutureBuilder to know weither or not the data is loading/fetched/error. And display something accordingly.