Updated 18 July 2023
In this blog, we will learn that How to use Shared Preferences to keep users logged in flutter?
Let’s imagine that if we are using an application after logging into the application.After using it sometimes, we quit or kill the app. Then ,when we open the app again and it asks to login again. Shit!!! It will be quite irritating to login again and again when we are opening the app.
People will avoid that application because of wastage of time on logging again and again.
Don’t worry ,.Here is the solution to this problem.And the solution is shared preference.
In this implementation, I am hoping that you have basic knowledge of shared preference.
You may also check our Flutter app development company page.
So, without wasting a single second, let’s start the stepwise code implementation to resolve this problem.
Add the Shared Preference dependency in your pubspec.yaml file.
1 2 |
dependencies: shared_preferences: ^2.0.15 |
After adding dependency, you can import the shared Preference package in your dart file.
1 |
import 'package:shared_preferences/shared_preferences.dart'; |
When we login ,we will save our email in the shared preference and after login we will open the app again. We can check that credentials that we have saved are not empty.
In this example, we have two screens, i.e.Login screen and Home screen.If we have the email, then the app will go on the home screen, else it will go on the login screen.
1 2 3 4 5 6 7 8 9 |
Future<void> main() async{ WidgetsFlutterBinding.ensureInitialized(); SharedPreferences prefs =await SharedPreferences.getInstance(); var email=prefs.getString("email"); print(email); runApp(MaterialApp( debugShowCheckedModeBanner: false, home: email==null?Login():Home(),)); } |
Let’s make the login screen and home screen.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
class Login extends StatelessWidget { const Login({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text("Login"),), body: Center( child: RaisedButton( color: Colors.green, onPressed: () async { SharedPreferences pref =await SharedPreferences.getInstance(); Navigator.pushReplacement(context, MaterialPageRoute(builder: (_){ return Home(); })); }, child: const Text("Login",style: TextStyle(fontWeight: FontWeight.bold,color: Colors.white),), ), ), ); } } class Home extends StatelessWidget { const Home({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text("Home"),), body: Center( child: RaisedButton( color: Colors.red, onPressed: () async { SharedPreferences pref = await SharedPreferences.getInstance(); pref.remove("email"); Navigator.pushReplacement(context, MaterialPageRoute(builder: (_){ return Login(); })); }, child: Text("Logout",style: TextStyle(fontWeight: FontWeight.bold,color: Colors.white),), ), ), ); } } |
On these screens, we have login and logout buttons respectively. At the click of Login button, user email will save in share preference.And on the click on logout, email will be removed from Shared Preference.
That’s it. It was all about code implementation.Now it’s time to check the output.
You can check the output in the below video.
You can see in the video that after logging in once, we are navigating on the home screen. We do not need to login again.
Congratulations!!!! You have learned how to use Shared Preferences to keep users logged in flutter?.
For more details and methods, you can refer to the official doc on flutter here.
For more interesting blogs check out here – https://mobikul.com/blog/
Hope this blog helps you with a better understanding in
Hope this blog helps you with a better understanding of how to use Shared Preferences to keep users logged in flutter?.
Thanks for reading 🙂
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
I Learn Your Shared Preferences blog and its very good to understand
Thank You…