Getx: [Question] General usability question

Created on 7 Jun 2020  路  7Comments  路  Source: jonataslaw/getx

Hey there,
I came across this package last night and thought it looked quite interesting. I am currently using a mix of the service locator package get_it, provider, and then event to handle watching/triggering of things outside the scope that provider can typically handle effectively as I am making a desktop application with underlying platform interactions, so a lot of my code ends up bring outside of the widget tree.

I found myself having to make little bridge functions between the three due to one thing being initialized via provider, then another one via get_it which ended up needing to share information. It just ends up being a hassle to build upon and maintain.

Am I understanding correctly that this package may be capable of offering the same if not more functionality than the three of those packages, but without the issues I have been working around?

Thanks,
-MH

Most helpful comment

MH, I've recently done exactly what you've asked about and it's simplified things greatly.
Here's how I replaced Get_It: Medium

All 7 comments

It can certainly do everything that get_it and provider does, and probably what the event does, in an easier way, but you won't need to pass data between one another, because all Get data is available inside and outside the tree. widgets.

MH, I've recently done exactly what you've asked about and it's simplified things greatly.
Here's how I replaced Get_It: Medium

MH, I've recently done exactly what you've asked about and it's simplified things greatly.
Here's how I replaced Get_It: Medium

Thanks

Thanks for the info and examples. I am working on testing it out by converting some things. I had a question, though. I am trying to set something up in a similar manner as I had before, so as not to have to completely change all of the logic on my end. How I was doing this before was, using Get_It and the Event package which had a broadcast mechanism to listeners, was something like this:

// - authstatus_event.dart -----
class AuthStatusListener {
  AuthStatus status = AuthStatus.signedOut;
  var event = Event();

  void setStatus(AuthStatus value) {
    this.status = value;
    print(this.status);
    event.broadcast();
  }
}
-----

// - loginpage.dart ----------
final sl = GetIt.instance;
--
final auth = sl<AuthStatusListener>();
auth.valueChangedEvent +
    (args) => () {
          if (auth.status == AuthStatus.verified) Future.microtask(() => _switchAuthMode(verify: true));
        }();

Is the proper way to go about it something like this?

// - firebase_auth.dart ----------
class FireBaseAuth extends GetController {
  static FireBaseAuth get to => Get.find();

  AuthStatus _status = AuthStatus.signedOut;
  get status => _status;
  set status(AuthStatus value) {
    _status = value;
    update();
  }
}

// - places and what not ----------
    var status = FireBaseAuth.to.status;
    ever(status, (s) => () {
              if (s == AuthStatus.verified) Future.microtask(() => _switchAuthMode(verify: true));
            }());

// Does it make a difference to do this?
    ever(FireBaseAuth.to.status, (s) => () {
              if (s == AuthStatus.verified) Future.microtask(() => _switchAuthMode(verify: true));
            }());

I am just wanting to make sure I have the hang of it, and that I don't go implementing things in such a way that it is going to bite me down the road, as I have a fairly decent amount of things I needed to change over, such as my message displaying system, which operates in a similar manner.

Hi, ever only works with reactive variables (.obs), you must use RxController with it, and you will not need the update() method.

I appreciate it, once again. I believe I should only have one last question for now. I was using the following:

MultiProvider(
      providers: [
        ChangeNotifierProvider.value(
          value: widget.messages ?? LoginMessages(),
        ),
        ChangeNotifierProvider(
          create: (context) => AuthState(
            onLogin: widget.onLogin,
            onSignup: widget.onSignup,
            onVerifyEmail: widget.onVerifyEmail,
            onRecoverPassword: widget.onRecoverPassword,
          ),
        ),
      ],

I then went to remove Provider to see what else in the code I needed to adjust when I came across these fellas. I imagine that I probably need to use a GetBuilder with an init for the AuthState, or is the GetBuilder only used if you are wanting to use a value right there within that builder's immediate scope?

I am also not quite sure what I would use from Get in place of the ChangeNotifierProvider.value? It either uses many different strings of text that are provided to the widget, or it will grab the defaults within the LoginMessages() class.

After these, though. I should be fully converted over and can remove all the other packages. 馃憤 It definitely is nice to be able consolidate things and remove a bunch of those intermediary classes.

GetBuilder / GetX is for two things:
1- Initialize and provide an instance that will be made available
2- Rebuild the necessary widget.

In fact when you use init: from GetBuilder, you are just using an "alias" for Get.put (YourDependecy());
You can start dependencies on GetBuilders/GetX or using Get.put in the build method or as a final variable of Stateless, or use Bindings for that. Everything will depend on your structure, and how you like to work.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

NO-ob picture NO-ob  路  4Comments

R-Praveen picture R-Praveen  路  4Comments

williamsilva-98 picture williamsilva-98  路  4Comments

NiranjanShah picture NiranjanShah  路  4Comments

Denilson-source picture Denilson-source  路  3Comments