Managing user sessions is a crucial part of mobile app development. In Flutter, one of the easiest and most effective ways to save login sessions is by using the SharedPreferences
package. This method allows you to store key-value pairs locally on the user’s device, making it perfect for saving simple data like login states or authentication tokens.

In this guide, you will learn how to implement login session management using SharedPreferences in Dart and Flutter. We’ll build a simple login flow that checks whether the user is already logged in and navigates to the home screen accordingly.
Note: Make sure you’ve added
shared_preferences
in yourpubspec.yaml
. You can find it on pub.dev.
📦 1. Authentication Service
This class handles the logic for saving and retrieving the login session.Managing user sessions is a crucial part of mobile app development. In Flutter, one of the easiest and most effective ways to save login sessions is by using the SharedPreferences
package. This method allows you to store key-value pairs locally on the user’s device, making it perfect for saving simple data like login states or authentication tokens.
In this guide, you will learn how to implement login session management using SharedPreferences in Dart and Flutter. We’ll build a simple login flow that checks whether the user is already logged in and navigates to the home screen accordingly.
Note: Make sure you’ve added
shared_preferences
in yourpubspec.yaml
. You can find it on pub.dev.
1. Authentication Service
This class handles the logic for saving and retrieving the login session.
import 'package:shared_preferences/shared_preferences.dart'; class AuthService { static const _keyLogin = "isLoggedIn"; Future<void> login() async { final prefs = await SharedPreferences.getInstance(); await prefs.setBool(_keyLogin, true); } Future<void> logout() async { final prefs = await SharedPreferences.getInstance(); await prefs.setBool(_keyLogin, false); } Future<bool> isLoggedIn() async { final prefs = await SharedPreferences.getInstance(); return prefs.getBool(_keyLogin) ?? false; } }
2. Main Entry Point (main.dart)
This determines the initial screen based on login status.
import 'package:flutter/material.dart'; import 'auth_service.dart'; import 'home_page.dart'; import 'login_page.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { final AuthService _authService = AuthService(); @override Widget build(BuildContext context) { return MaterialApp( title: 'Login Session Demo', home: FutureBuilder<bool>( future: _authService.isLoggedIn(), builder: (context, snapshot) { if (snapshot.connectionState == ConnectionState.waiting) { return Scaffold(body: Center(child: CircularProgressIndicator())); } else { return snapshot.data == true ? HomePage() : LoginPage(); } }, ), ); } }
3. Login Page (login_page.dart)
Handles user login and session save.
import 'package:flutter/material.dart'; import 'auth_service.dart'; import 'home_page.dart'; class LoginPage extends StatelessWidget { final AuthService _authService = AuthService(); void _handleLogin(BuildContext context) async { await _authService.login(); Navigator.pushReplacement( context, MaterialPageRoute(builder: (_) => HomePage()), ); } @override Widget build(BuildContext context) { return Scaffold( body: Center( child: ElevatedButton( onPressed: () => _handleLogin(context), child: Text('Login'), ), ), ); } }
4. Home Page (home_page.dart)
Displays content for logged-in users and handles logout.
import 'package:flutter/material.dart'; import 'auth_service.dart'; import 'login_page.dart'; class HomePage extends StatelessWidget { final AuthService _authService = AuthService(); void _handleLogout(BuildContext context) async { await _authService.logout(); Navigator.pushReplacement( context, MaterialPageRoute(builder: (_) => LoginPage()), ); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text("Home")), body: Center( child: ElevatedButton( onPressed: () => _handleLogout(context), child: Text('Logout'), ), ), ); } }
This method provides a lightweight and reliable way to persist login states without needing a backend. Ideal for simple apps or prototyping, using SharedPreferences
in Dart is fast and efficient.