Reputation: 39
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:
Upvotes: 0
Views: 793
Reputation: 39
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.
Upvotes: 1
Reputation: 1514
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);
});
Upvotes: 0