Created
September 30, 2023 13:21
-
-
Save Abhishek12345679/9009010c29bbfde0ef9a6f5d15ab3f28 to your computer and use it in GitHub Desktop.
Flutter Logout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ignore: must_be_immutable | |
class HomePage extends StatefulWidget { | |
const HomePage({ | |
super.key, | |
}); | |
@override | |
State<HomePage> createState() => _HomePageState(); | |
} | |
class _HomePageState extends State<HomePage> { | |
final authBloc = AuthBloc(); | |
@override | |
Widget build(BuildContext context) { | |
return BlocConsumer<AuthBloc, AuthState>( | |
bloc: authBloc, | |
listenWhen: (previous, current) => current is AuthActionState, | |
buildWhen: (previous, current) => current is! AuthActionState, | |
listener: (context, state) {}, | |
builder: (context, state) { | |
return Scaffold( | |
appBar: AppBar( | |
title: const Text('Home'), | |
), | |
body: Column( | |
children: [ | |
ElevatedButton( | |
onPressed: () async { | |
authBloc.add(AuthLogoutEvent()); | |
final prefs = await SharedPreferences.getInstance(); | |
prefs.remove('user_token'); | |
prefs.remove('user_id'); | |
prefs.remove('refresh_token'); | |
prefs.setBool('loggedIn', false); | |
if (mounted) { | |
Navigator.pushReplacement( | |
context, | |
MaterialPageRoute( | |
builder: (context) => const PhoneNumberScreen(), | |
), | |
); | |
} | |
}, | |
child: const Text("log out"), | |
), | |
], | |
), | |
); | |
}, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment