Flutter auth firebase getx setstate() called after dispose

215

You can stop listening to the animation in the dispose() callback or cancel the timer.

Another solution is to check the mounted property of the state class of your widget before calling setState(). For Example:

 if (mounted) {
  setState(() {
    // Your code here
  });
}
Share:
215
David Meléndez
Author by

David Meléndez

Updated on January 04, 2023

Comments

  • David Meléndez
    David Meléndez over 1 year

    I'm trying to create a flutter app with getx. Particularly in the authentication section, I have the following error:

    E/flutter ( 8992): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: setState() called after dispose(): _LogButtonState#3e8d6(lifecycle state: defunct, not mounted) E/flutter ( 8992): This error happens if you call setState() on a State object for a widget that no longer appears in the widget tree (e.g., whose parent widget no longer includes the widget in its build). This error can occur when code calls setState() from a timer or an animation callback. E/flutter ( 8992): The preferred solution is to cancel the timer or stop listening to the animation in the dispose() callback. Another solution is to check the "mounted" property of this object before calling setState() to ensure the object is still in the tree. E/flutter ( 8992): This error might indicate a memory leak if setState() is being called because another object is retaining a reference to this State object after it has been removed from the tree. To avoid memory leaks, consider breaking the reference to this object during dispose(). E/flutter ( 8992): #0 State.setState. (package:flutter/src/widgets/framework.dart:1085:9) E/flutter ( 8992): #1 State.setState (package:flutter/src/widgets/framework.dart:1120:6) E/flutter ( 8992): #2 _LogButtonState.build. (package:sneakychat/Screens/Authentication/login.dart:231:9) E/flutter ( 8992): E/flutter ( 8992):

    The code for the authentication controller is the following:

    import 'package:firebase_auth/firebase_auth.dart';
    import 'package:firebase_core/firebase_core.dart';
    import 'package:flutter/material.dart';
    import 'package:flutter_facebook_auth/flutter_facebook_auth.dart';
    import 'package:flutter/cupertino.dart';
    import 'package:get/get.dart';
    import 'package:google_sign_in/google_sign_in.dart';
    
    import '../Globals/globals.dart';
    
    class AuthController extends GetxController {
      final FirebaseAuth _auth = firebaseAuth;
    
      late Rx<User?> _firebaseUser;
      late Rx<GoogleSignInAccount?> _googleSignInAccount;
    
      //UserModel get user => _firebaseUser.value;
    
      late GoogleSignIn googleSignIn = GoogleSignIn();
      //var a = googleSignIn.currentUser;
    
      @override
      onReady() {
        super.onReady();
        print("##########Im Reasdy###########");
        googleSignIn = GoogleSignIn();
    
        // Load current user
        _firebaseUser = Rx<User?>(_auth.currentUser);
        _googleSignInAccount = Rx<GoogleSignInAccount?>(googleSignIn.currentUser);
    
        // Bind Streams for listeners
        _firebaseUser.bindStream(_auth.userChanges());
        _googleSignInAccount.bindStream(googleSignIn.onCurrentUserChanged);
    
        // Call workers to update auth state
        ever(_firebaseUser, _manageAuthState);
        ever(_googleSignInAccount, _manageAuthStateGoogle);
      }
    
      // Manage the auth state
      _manageAuthState(User? user) {
        print("Firebase auth state active :D");
        if (user == null) {
          Get.offAllNamed("/LogIn");
        } else {
          Get.offAllNamed("/Home");
        }
      }
    
      // Manage the auth state regarding google's user state
      _manageAuthStateGoogle(GoogleSignInAccount? googleSignInAccount) {
        print("Google auth state active");
        if (googleSignInAccount == null) {
          Get.offAllNamed("/LogIn");
        } else {
          Get.offAllNamed("/Home");
        }
      }
    
      // Sign with google account
      Future<void> signupGoogle() async {
        final GoogleSignIn googleSignIn = GoogleSignIn();
        try {
          final GoogleSignInAccount? googleSignInAccount =
          await googleSignIn.signIn();
          _googleSignInAccount.value = googleSignInAccount;
    
          if (googleSignInAccount != null) {
            final GoogleSignInAuthentication googleSignInAuthentication =
            await googleSignInAccount.authentication;
            final AuthCredential authCredential = GoogleAuthProvider.credential(
                idToken: googleSignInAuthentication.idToken,
                accessToken: googleSignInAuthentication.accessToken);
    
            // Bind Google account with Firebase account
            UserCredential resultUser = await _auth.signInWithCredential(
                authCredential);
    
            print(resultUser.credential);
            // Set a listener for the user
            _firebaseUser.value = resultUser.user;
            print(resultUser.user);
    
            // Store user data if its new
            /*User user = User(
              name: username,
              email: email,
              uid: cred.user!.uid,
              profilePhoto: downloadUrl,
            );
            await firestore
                .collection('users')
                .doc(cred.user!.uid)
                .set(user.toJson());*/
    
            Get.offAllNamed("/Home");
          }
        } catch (exc) {
          print(exc);
          Get.snackbar(
              'Something went wrong',
              "We couldn't connect with google's server",
              snackPosition: SnackPosition.TOP,
            backgroundColor: Colors.amber,
            colorText: Colors.white
          );
        }
      }
    
      Future<void> logoutGoogle() async {
        print("Sign out!");
        await googleSignIn.signOut();
        await _auth.signOut();
    
        print(googleSignIn.currentUser?.email);
        print("#################################");
        print(_firebaseUser.value?.uid);
        print("#################################");
        //Get.offAllNamed("/LogIn");
      }
    
      signIn() {}
    }
    
  • David Meléndez
    David Meléndez about 2 years
    what worker should i dispose? The ever(_firebaseUser, _manageAuthState) or ever(_googleSignInAccount, _manageAuthStateGoogle).
  • David Meléndez
    David Meléndez about 2 years
    I'm afraid I'm not following you. What timer am I listening to?
  • Radadiya Nikunj
    Radadiya Nikunj about 2 years
    Please refer following answer: stackoverflow.com/questions/49340116/…