Skip to content

Instantly share code, notes, and snippets.

@Abhishek12345679
Created September 30, 2023 13:21
Show Gist options
  • Save Abhishek12345679/9009010c29bbfde0ef9a6f5d15ab3f28 to your computer and use it in GitHub Desktop.
Save Abhishek12345679/9009010c29bbfde0ef9a6f5d15ab3f28 to your computer and use it in GitHub Desktop.
Flutter Logout
// 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