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
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,
hope this helps
Your code seems right, however you need to navigate it back to the login page after the signing out.