skip to Main Content

im using firebase auth, and choose the password and email authentication method.
logging in works fine however i cant seem to allow users to sign out.
there are no errors in my debug console but this is displayed

D/FirebaseAuth(22582): Notifying id token listeners about a sign-out event.
D/FirebaseAuth(22582): Notifying auth state listeners about a sign-out event

this is my main.dart

import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart';

import 'package:imporved_main_screens/screens/login.dart';
import 'package:imporved_main_screens/screens/loading.dart';
import 'package:imporved_main_screens/screens/dummy_home.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  );

  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: ThemeData(fontFamily: 'Poppins'),
      home: StreamBuilder(
        stream: FirebaseAuth.instance.authStateChanges(),
        builder: (ctx, snapShot) {
          if (snapShot.hasData) {
            return const DummyHome(); //later change this to actual home page screen
          }
          if (snapShot.connectionState == ConnectionState.waiting) {
            return const Loading();
          }
          return const LogInScreen();
        },
      ),
    );
  }
}

this is dummy_home.dart

import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';

import 'package:imporved_main_screens/theme.dart';
//import 'package:imporved_main_screens/widgets/register_form.dart';

String? _errorMessage;

class DummyHome extends StatelessWidget {
  const DummyHome({super.key});

  Future<void> _signOut() async {
    try {
      await FirebaseAuth.instance.signOut();
    } catch (e) {
      print('Error signing out: $e');
    }
  }

  @override
  Widget build(BuildContext context) {
    _errorMessage = ModalRoute.of(context)?.settings.arguments as String?;
    return Scaffold(
      appBar: AppBar(
        title: const Text('Home Page'),
        actions: [
          IconButton(
            onPressed: _signOut,
            icon: const Icon(Icons.exit_to_app, color: kPrimaryColor),
          )
        ],
      ),
      body: const Center(
        child: Text('Insert grids'),
      ),
    );
  }
}

p.s im a bigenner and i was just following a course content that is recent to this year, i also used chatgpt, looked up possible solutions, followed them and the problem have not been resolved.

2

Answers


  1. if your are not getting any errors it must be working as expected ,
    check your firebase instance user data exists or not if not then it indicates user is signed out,

    final FirebaseAuth _auth = FirebaseAuth.instance;
      if (_auth.currentUser != null)  // Check if user is signed in
    {
    //stay
    }else{
    //navigate to login screen
    }
    

    hope this helps

    Login or Signup to reply.
  2. Your code seems right, however you need to navigate it back to the login page after the signing out.

    FirebaseAuth.instance.signOut();
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(
        content: Text('Signed out successfully.'),
        duration: Duration(seconds: 2),
      ),
    );
    
    // Navigate back to the login page after signing out
    Navigator.of(context).pop(); // Close the home page
    Navigator.of(context).pushReplacementNamed('/login');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search