0

I have questions regarding singing out a user while using Firebase Authentication (e-Mail & password) on a Flutter Application (iOS). I searched a lot but I just can't get the last bit of understanding my problem.

So in my app I have multiple Screens where I route through with the Navigator.pushNamed(...). The User is able to navigate to a settings screen where he should be able to log out.

class _OptionScreenState extends State<OptionScreen> {
  FirebaseAuth _auth = FirebaseAuth.instance;

  _signOut() async {
    try {
      await _auth.signOut();
    } catch (e) {
      print(e.toString());
    }
  }

  @override
  Widget build(BuildContext context) {
           (...some more code here....)
            ListTile(
              title: Text(
                'Logout',
                style: TextStyle(fontSize: 25.0),
              ),
              onTap: () async {
                await _signOut();
               Navigator.pushReplacementNamed(context, 'welcome_screen');
              },

The Welcome_Screen is the starting screen where the user is able to choose between logging in or singing up.

After being pushed to the welcome screen I get the following error:

Receiver: null Tried calling: uid flutter: NoSuchMethodError: The getter 'uid' was called on null.

So my questions are:

  1. I create the authentication Instance everywhere I need it. Do I need one global Instance or do I have to sing out every instance I created? I have the feeling that other "screens" are still active and after logging of they miss an active user (I hope this is understandable) if true: Can I close those screens? So that the user starts with a "fresh" app after logging out?
  2. Is there a solution to that (haha)
  3. Should I use Provider to share one single _auth instance?
0

2 Answers 2

1

So after trying to fix this problem for some time I found the problem:

I had some functions in the background of my Login Page which loaded the user information so everything is ready as soon as the user is done with logging in.

Problem was that some functions where called while building the login page and after logging out there was no user to load the information from.

I placed the functions after the user signed in successfully and now everything is loading fine and the user can logout without any error. Now I can ensure there is a signed in user when calling those functions.

On thing I cannot explain is why the error was shown on the welcome_screen because there are no functions inside this screen and the order is: Welcome_screen -> login_screen. So normally the login screen isn't loaded when I close everything down to the welcome screen. But still the error is gone.

Thanks to @Lunedor for showing me how to close all previous screens.

Sign up to request clarification or add additional context in comments.

1 Comment

if there is a navigate order when user login in your welcome page or any case you checked is logged in or not, background screens or data would assume still logged and try to navigate to next page or process login info so that could be the reason for first error.
0

I think your problem related with your welcome screen, maybe to see it's code would be better. But just an quick respond you can check uid is null or not in this welcome screen and if it is null you can lead your process while not login.

And for close all active screens you can use below code instead of navigate to your welcome screen, '/' means root page so you can write your page name there:

SchedulerBinding.instance.addPostFrameCallback((_) {
    Navigator.of(context).pushNamedAndRemoveUntil(
        '/', (Route<dynamic> route) => false);
  });

1 Comment

Thanks for your response. To see if the welcome screen causes to problem I called to signout() function without navigating to another screen. Sadly the error is still the same. So the Error seems nor related to the welcome Screen. I implemented your code for closing all screens and I think it works fine, thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.