In this tutorial we shall discuss how to create the logout screen of out flutter app.
- Create The Logout Screen
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
class LogoutScreen extends StatefulWidget {
const LogoutScreen({super.key});
@override
State<LogoutScreen> createState() => _LogoutScreenState();
}
class _LogoutScreenState extends State<LogoutScreen> {
@override
Widget build(BuildContext context) {
return const Placeholder();
}
}
2. Now Create The Asynchronous Function
import 'package:shared_preferences/shared_preferences.dart';
import '../Home/Home.dart';
Future<void> _logoutUser() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.remove('email');
await prefs.remove('password');
// Redirect to Home screen
Navigator.pushReplacement(
context,
MaterialPageRoute(builder: (context) => Home()),
);
}
3. Now Call The Logout Function On Page Load
@override
void initState() {
super.initState();
_logoutUser();
}