jonataslaw / getx

Open screens/snackbars/dialogs/bottomSheets without context, manage states and inject dependencies easily with Get.
MIT License
10.2k stars 1.6k forks source link

GetxService documentation and examples #724

Open sashker opened 3 years ago

sashker commented 3 years ago

Hello.

In my app I need exactly to use controllers that exist during the lifespan of the app. Currently, I'm using Get.put(SomeController(), permanent=true) and I operate with Get.reset() and Get.destroy().

Recently I read about GetxService which seems to be a better way to manage permanent controllers. But current documentation is not clear, in my opinion, on: 1) If services have no logic inside - what is the relationship between services and controllers (where all logic lives, I believe) 2) what is the difference between some controller with permanent=true and a service 3) I'm not quite sure how to use cross-referenced async controllers (or services) it's a good way to do it at all.

I'd love to help with some documentation or examples after I understand how to use it properly.

roipeker commented 3 years ago

1-If service has no logic inside is just a global class with maybe some static vars inside. Thats not a "service" 2-you can force delete a controller marked as permanent, you cant remove a service. 3-thats the idea of a service, yet it should sit on top of all the other logic layers, like controllers.

So, a service is meant to be the last layer of business logic where u interact, mostly, with the outside of ur dart code. Meaning, APIService has all the logic to deal with http requests, StorageService might be using some local db, SettingsService must use some smaller key/value storage like sharedpref/hive, MediaService might be your access point to retrieve local files or make some processing on media files. You get the idea I guess. According to the architecture u follow in your code, Controllers communicates with "Provider classes" (intermediary classses to process Services data or manage some local cache) which communicates with Services layers, the "metal" layer of the system.

A service has no clue about providers or controllers... controllers have lifecycles and are somehow, tightly coupled to your views (widgets), they meant to manage the business logic of the UI layer, thats why GetX used instead of Stateful widget to keep the code cleaner and separated.

Services are usually inited before you runApp() and are never destroyed... a controller, in the "common flow", should be destroyed when the creator widget (GetBuilder or GetX) is unmounted or the navigation route where it was assigned to is popped.

I hope this long reply clarifies you the idea of a Service in GetX.

arcas0803 commented 3 years ago

So a service is a global controller that you have to inject via get.find() in your controllers? Controllers for interact with ui. Service as a layer between controllers and providers?

eduardoflorence commented 3 years ago

@arcas0803 you are correct that GetxService is a global controller, but depending on your architecture it is usually injected into the provider and not the controller of a view, for example a GetxService that initializes the connection to the database (SQLite).

arcas0803 commented 3 years ago

Can you show a little example. If you want to separate the layers in your project you should not inject a service into the provider or am i wrong?

xyzbilal commented 3 years ago

Guys, I have a softphone app that build on webrtc and communicates with janus gateway. also it has some pages that call center agents fills some forms while talking on phone. as GetxService class persistent in memory, should I consider if app goes to backround getxservice class will continue to run or it is only runs when app is only foreground?

mjablecnik commented 3 years ago

@arcas0803 Hello, I have a little project where I am using GetxService and GetxControllers here: https://github.com/mjablecnik/flutter_getx_timetracker

So you can look there how I am using it.

https://github.com/mjablecnik/flutter_getx_timetracker/blob/master/lib/controllers/tracker_list.dart https://github.com/mjablecnik/flutter_getx_timetracker/blob/master/lib/data/repositories/tracker.dart

Basically GetxController I am using for manage the business logic of the View. And GetxService I am using as repository for obtain data from sqlite database but it can be used also for obtain data from server through API or for connection with firebase or something else what you don't want to have in the controller or can be used by more controllers.. :) ;)

gOzaru commented 2 years ago

@arcas0803 Hello, I have a little project where I am using GetxService and GetxControllers here: https://github.com/mjablecnik/flutter_getx_timetracker

Great, dude. I really need this one. Now your name will be included as contributor in my project. Anyone who made good plugin for Flutter, and people who made tutorials on them will receive reward from project I have been working at. You help me, I will reward you. I will only send money if you have Sponsor in Github or a valid Patreon/Ko-fi account stated in your Github/tutorials website.

mjablecnik commented 2 years ago

@gOzaru Hello I am very pleasured that my code is useful for you :) Unfortunately I don't have Github Sponsor, Patreon or Ko-fi accounts but you can donate me via PayPal: paypal.me/mjablecnik Thank you very much it will be very big motivation for my another work ;)

gOzaru commented 2 years ago

@gOzaru Hello I am very pleasured that my code is useful for you :) Unfortunately I don't have Github Sponsor, Patreon or Ko-fi accounts but you can donate me via PayPal: paypal.me/mjablecnik Thank you very much it will be very big motivation for my another work ;)

No problem. This reward program is only for contributors of dart packages for Flutter and people who made tutorials website about it, like yours. I will let you know once my revenue reaches out the first million dollar. All of contributors will receive a total of USD100K for maximum of 5 years, based on their contribution in my project. If you have community group, please share this message and tag my Github account, or pm me; so I can know it and use it for my project. Actually, I also need example of how to use Cloud Run and Cloud SQL using Flutter.