jonataslaw / getx

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

Using Get as state manager & DI for firebase auth #142

Closed TJMusiitwa closed 4 years ago

TJMusiitwa commented 4 years ago

Hey there great great package. I am currently using this package in combination with Firebase to handle to prime areas of business logic being authentication and firestore.

I did set up the base class for firebase auth as stated under the docs for state management.

class AuthService extends GetController{
final FirebaseAuth _firebaseAuth = Get.put(FirebaseAuth.instance);
AuthService authservice = Get.put(AuthService());

Future loginwithEmail({ String email, String password}) async {
try {
    var user = await _firebaseAuth.signInWithEmailAndPassword(email:email,password:password);
    }catch (e){
    return e.message;
    }
}

When I then attempt to use this in my loginScreen Page as follows and I think here is where the error is showing up from

//Around my submit button
GetBuilder<AuthService>(
init:AuthService(),
builder:(auth){
return RaisedButton(
child: Text('Login'),
onPressed:() async {
    return _loginFormKey.currentState.validate()
    ?auth.loginWithEmail(
    email:_emailController.text,
    password:_passwordController.text).then((value))=> Get.off(HomeScreen())
    :'Get.snackbar('Please fix any errors and try again');
    });
  }
)

And this is my main.dart file

class MyApp extends StatelessWidget{
final bool currentUser = Hive.box('vendorBox').get('currentUser', defaultValue:null);
@override
Widget build(BuildContext context){
return GetMaterialApp(
title: 'Vendor App',
home: currentUser == true ? HomeScreen() : LoginScreen(),
    );
  }
}

So what am I having to do wrong thus far and what should I change to get it working? Thank you

jonataslaw commented 4 years ago

Hi, thanks, I will try help you. It's typo?

 password:_passwordController.text).then((value))=> Get.off(HomeScreen())
    :'Get.snackbar('Please fix any errors and try again');

Before Get there are single quotes, and Get.snackbar does not have a title

Get.snackbar('Errors found','Please fix and try again');
TJMusiitwa commented 4 years ago

Hey yes that was just a typo 😁. The rest is as is. Please continue

jonataslaw commented 4 years ago

Please paste the error code so I can try to help you.

TJMusiitwa commented 4 years ago

Hey so I was able to fix the above error but then not sure if it is because of the way I set up my code but I am unable to perform the sign out method

In my main method: void main() async { Get.lazyPut<AuthService>(() => AuthService()); runApp(MyApp()); }

In my authservice:

static AuthService get authServ => Get.find(); Future signOutUser() async { await _firebaseAuth.signOut(); return Future.delayed(Duration.zero); }

Then the raisedbutton to call my signout method: onPressed: () { AuthService.authServ.signOutUser().then((value) { Hive.box('kaziVendorBox').delete('currentUserIsTrue'); return Get.off(LoginScreen()); }); },

I get back the following error:

The following message was thrown while handling a gesture: I/flutter (28753): AuthService not found. You need call Get.put(AuthService()) before

So should I again wrap my raised button with the GetBuilder callback to avoid the above error or is there some other way I should be handling this?

jonataslaw commented 4 years ago

This is because Get.offAll will take all of your controllers out of memory. The Route Manager notifies the dependency manager that you have used a method that will "clean up" your stack, and SmartManagement will close all your Controllers registered on your routes, and delete them all. The first way to fix this is to "circumvent" SmartManagement using Get.put before GetMaterialApp (usually on main) and registering a dependency before it is in action (when you use lazy, it will only be created later, when GetMaterialApp is already will be in effect, and some route has already been registered, using Get.put, the instance will be registered immediately, which means that it will be registered without any link with SmartManagement if this happens before GetMaterialApp):

Get.put<AuthService>(AuthService());instead Get.lazyPut<AuthService>(() => AuthService());

Or simply use Binding Get.off(Container(), binding: LoginBinding());

TJMusiitwa commented 4 years ago

I will be sure to give this a try and update you. Right now, I am currently balancing a multitude of projects.

jonataslaw commented 4 years ago

There are some examples in closed issues of how to use firebaseAuth with ever. Too easy. Without further details, it will not be possible to proceed with this issue, which is basically about configuring your project. If you face problems, you can open another issue.