flutter best practices on initstate() - flutter

I am new to flutter and I have been reading about initstate but it's not clear to me how to use and if it's the best solution for what I am trying. There might be other best options I am sure, but I am after some best practices.
I have an specific problem within application that performs some social login and other activities. When I start the application I want to validate if the user is logged, then, if the user is logged return the user to initstate (not sure if that's possible) in order to retrieve more information about that user (i.e if that user has any notifications, etc...) within the initstate function.
I am using initstate within the splashscreen function, so I want to retrieve as much information I can (within the 3s splashscreen best practices ) in order to make the rest of the app smoother. But not sure if it's it's the best way to do it
thanks

Related

Can I use deep linking in flutter as a trigger?

I am deep linking into my app, or at least trying to. Let's make a fictional example:
// my clock app
myapp://timer?102seconds
If I understand deep links correctly, they kind of work like URLs on websites. Go to the page described in the link. This I don't need and honestly I don't think it would work even. I have a few screens on the App which I want to be able to be called like a Trigger.
In the example above I want the app to receive timer?102seconds, so that my app itself can
-be opened with the uri
is it a timer or a stopwatch or an alarm
if it is a timer, how long should it be
push the timer page on the navigator
give the bloc the info of time that I need here
I am not sure how possible that is, but it is the easiest way I can imagine implementing this here. Thanks!
If you use Firebase Dynamic Links you can encode another link, from which you can extract data. Check out this link to see documentation on how to do that.

Can i call cubit from another cubit?

i'm working on an application that uses cubits, to manage the state.
Very often when i need to make an api request i need to get my current localization (handle possible exceptions) and then send it to backend. So i guess i should use cubit for the requests and for getting my current localization.
So now my question is how should i handle that? Can i somehow call gpsLocalizationCubit from another cubit? Should i call gpsLocalizationCubit and on success call requestCubit with the use of bloc listener? But then how should i manage loading screen that should be visible for both getting localization and api request?
Another problem is that i have multiple similiar request (that need to use current localization) in single view.
Thanks for the answers :D

Flutter is using provider to load data is the right option?

So i have a situation where i make an request from the server for one widget.
The widget is at the home page, lets take the worst case where the data is huge and the request take time.
Should i change the widget to stateless and make a provider which i will initialize before i run the app with all the initial data?
Should i contain all the data of the widgets at home page and deliver theme as props, i miss understood the concept of managing the state here, I'm coming from vue and i try to write my first app and I'm struggling how to structure my data through the routes.
I would like if some one explain or give a good source that show how to initialize data from third party
before the home page reload.
Which approach is better getting all the app data before the app reload or request data every time from db with cash
You might have seen this approach in other apps as well which is to show a splash screen until the data has been loaded and ready to be shown. This approach is mostly used by apps which got large data to load at the start. You could achieve this in your initState like the following.
#override
void initState() {
loadData();
splashTimer = Timer(Duration(seconds: 4), () {
_goToHome();
});
super.initState();
}
State management in flutter is a topic with hot debate, there is no best approach, but using one for sure is better than nothing. However there are exceptions to this, sometime adding a state management to a simple part of the app is not recommended. Regarding your case, it can be done without a full state management solution, by using a FutureBuilder for example. Or it can be also done with Provider, BloC, Redux...
As a naïve general rule, if the state is to be passed down the widget tree more than 1 or 2 levels, you should probably start looking for a state management solution depending on the use case. As I already have said, there is no one best state management solution.
Also, it is ok to use more than one as long as you know what you are doing but in general as a best practice it is not recommended to use more than one.
Regarding the second part of the question, it totally depends on the nature of the data and it's size. If the data is big and it is a small possibility that the user will be using all of it, it is better to load it on demand, also loading all the data upfront will increase the cost on the backend side.
However getting the data upfront, makes the experience more seamless to the user (Not waiting while using the app, but he will have to wait a little extra when the app is first loading).
So as you see it is a balance. Also it is good for the server and the app to do some type of caching since it helps reduce the work on the server side and decrease the bandwidth usage on the phone.
An example for caching images you can use Cached Network Image Link, example from flutter cookbook Link.

Flutter correct way to deal with singleton

I understand that ideally if you want to pass around some value like user profile, InheritedWidget comes into play.
But since InheritedWidget can't be accessed from initState (I got error).
If you need to access user profile in initState, how should I handle this?
Should I create static variable, SharedPreferences or any other way?
Is there a way to do this with InheritedWidget?

Is there a way to call specific code right before the app is killed or moves to the background?

I want to write the user's preferences to a file before they leave the app, so I'm looking for something in Flutter like Android's onPause() or onStop() methods. Is this something so platform-specific that I'd need to write services for it and actually use Android/iOS's specific methods for these situations or is there a way to do it only using Flutter/Dart?
My understaning is that this is possible with the didChangeAppLifecycleState callback on the WidgetsBindingObserver:
https://docs.flutter.io/flutter/widgets/WidgetsBindingObserver-class.html
Examples:
https://github.com/flutter/flutter/search?utf8=%E2%9C%93&q=didChangeAppLifecycleState
We definitely need some better docs and examples here. I've filed:
https://github.com/flutter/flutter/issues/7394
If these are not sufficient for your needs, it is also possible to listen for any events in your Objective-C or Java code and forward those along to Dart via HostMessages (documented at https://flutter.io/platform-services).
first you need to add with WidgetsBindingObserver
then dont forget to init & dispose the WidgetsBindng
then you can call it from didChangeAppLifecycleState