Using getX in Flutter with a singleton service - flutter

Using getX in Flutter, suppose I need to use the same service for different controllers.
For example, the same DB service for both UsersController and ProductsController.
What would be a best practice to do that?
Creating a singleton DB service?
Using getIt with the DB service?
Some other getX trick?
GetxService?

It would be more accurate to create and use a singleton object with the following method.
Get.put<LoginService>(LoginService(), permanent: true);
Get.find<LoginService>();
GetX Documentation about Get.put
the class that you want to get to save, like a controller or anything
// note: "S" means that it can be a class of any type

Related

Basic question about firebase real time database. Is there a good pattern for hiding the implementations from the UI?

Many of the online tutorials I see for firebase show hard coded implementations in the GUI classes, for flutter, widget classes, for Android, the activity class.
Is it redundant to provide another layer of abstraction for the realtime database?
I know firebase is accessed as a singleton. Can I extend it in some way? I want to hide the code and firebase class set() and update() methods, etc
I just want to be able to swap my database out for another one later on down the line if need be.
Could I extend the firebase class, or get the instance in a separate DatabaseFacade and pass it an interface reference for GUI or widget callbacks?
I usually access the database directly in my widgets simply because it's so convenient.
But I also have a bigger app where I wrapped everything in DAO/DTO classes, and that works fine too. There is no way to extend the Firebase classes that I know off though, so I started with a central Manager class that then provides access to al the data access methods and classes.

Alternative to GetX ever() method using Provider

I want to move away from GetX, but ever() is a method I rely heavily upon. I wanted to switch to provider, but it doesn't seem like provider have a listener method like GetX does. Does Flutter or Provider have a built in method similar to this? Or other alternative packages? I know Flutter has a .addListener() method, but that is only used for animation and such, not on variable.

What's the proper way to dependecy inject with Provider?

I'm building an app and want to use Provider as my to-go DI tool.
I want to build an app with a following architecture:
Views
ViewModels
Sevices
Views are plain simple UIs, supported by viewmodels for (almost) every view. And these viewmodels are dependent on values, which are held by services.
Viewmodels are extended with ChangeNotifierProvider to reflect changes in the UI, and this works okay. But what should I use to be responsive to the changes in the services. Single service can be used in multiple viewmodels and, sure, I need to keep all of them up to date.
I've looked up some tutorials, dugged in Flutter Provider v3 Architecture using ProxyProvider for Injection, but it really doesn't explain to me this kind of this or I don't get it.
ProxyProvider is used for other cases there. Viewmodel makes requests to the service, but it's values are consumed directly in the UI.
What's the proper way to make this kind of connection work? Should I use ChangeNotifierProxyProvider for such case? Should I use streams?
To use di as you say there is ProxyProvider; ProxyProvider can be as app scoped (declared on top of App widget so he can be accessed anywhere in-app For example AuthService) or widget (screen) scoped for example your ViewModel
In this example, A is ViewModel of MyWidget and B is Service
Ps. you should provide another Provider with B class on top of this hierarchy so ProxyProvider can access it to "provide" to A class
ProxyProvider<B, A>(
create: (_) => A(),
update: (_, b, a) => a..bClassVariable = b,
dispose: (_, a) => a.dispose(), //if you use streams
child: MyWidget(
),
)
About your last answer there is no answer) only you should decide which approach is better for your needs, If you familiar with ViewVievModel you can stay with it, or learn something new like BloC
Ps. If you use ViewModel ChangeNotifierProxyProvider will work fine
and if you use bloc streams should be used

Flutter Provider: "ProxyFutureProvider" or similar

As I understand the concept of Proxy Providers allows us to use the value of another provider in another. How does one inject the value of a FutureProvider into another FutureProvider? I thought there would be a FutureProxyProvider, but there doesn't seem to be one.

Flutter : Is provider an alternative to the BLoC pattern?

I know that BLoC in flutter acts like the viewmodel layer in android's MVVM, so the data does not gets fetched again and again upon configuration changes (for ex: change in screen orientation).
I am confused if provider replaces the functionality of RxDart in BLoC pattern or it replaces the role BLoC pattern itself.
Also, if I don't use BLoC at all an only providers does the app survives configuration changes.
Please explain what are the limitations of provider over BLoC, RxDart combination with some use cases.
Provider in itself doesn't replace the BLoC pattern. However, you can set up your architecture to use Provider in a way that could replace the BLoC pattern.
One way to do that would be to use the MVVM pattern, where you have a view model class that extends a ChangeNotifier. Then you can listen to that view model with a ChangeNotifierProvider so that the UI gets rebuilt any time the view model changes. FilledStacks does that well here.
See also
A beginner’s guide to architecting a Flutter app
State Management With Provider
Personally I find it easier to use the builtin Flutter tools to manage state. I describe that more here:
Flutter state management for minimalists