In this tutorial we will learn how to use Session In Flutter. Saving and getting values into session using the flutter session package.

What Is Session?
If any value saved in session is true, it can not be false until you will set to false it using the session. Either you close the application of destroy the screen. Session saves the values that can be access from anywhere in the app. Mainly sessions are used to save the login credentials.
Add Flutter Session Package:
To use the sessions, first add the following dependencies in the pubspec.yml file. It will look like this:
Note: You can get latest dependency from here.
dependencies:
flutter:
sdk: flutter
flutter_session: ^0.1.1

Import The Library
import 'package:flutter_session/flutter_session.dart';
Set And Get The Session Values:
To set the values into session use following way ->
var session = FlutterSession();
session.set("isLogin", true);
Get The values from the session ->
bool isLogin = await FlutterSession().get("isLogin");
Complete Example For Session In Flutter
To understand in easy way, we created an example see below.
Splash.dart
In this class we set the session values as in bool type. And also get those value. For clear understand, we created a text which shows the session value true or false.
import 'package:flutter/material.dart';
import 'package:flutter_session/flutter_session.dart';
import 'package:flutter_testing_project/HomePage.dart';
class SplashScreen extends StatefulWidget {
const SplashScreen({Key key}) : super(key: key);
@override
_SplashScreenState createState() => _SplashScreenState();
}
class _SplashScreenState extends State<SplashScreen> {
var session = FlutterSession();
bool isLogin = false;
String text = "";
@override
void initState() {
// TODO: implement initState
super.initState();
_getSession();
print("session == " + isLogin.toString());
}
_getSession() async {
isLogin = await FlutterSession().get("isLogin");
setState(() {
text = isLogin.toString();
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Title"),
),
body: SingleChildScrollView(
child: Container(
child: Column(
children: [
Text("This is splash screen"),
Text("session = " + text),
Image.network(
'https://helpx.adobe.com/content/dam/help/en/photoshop/using/convert-color-image-black-white/jcr_content/main-pars/before_and_after/image-before/Landscape-Color.jpg',
width: 200,
height: 200,
fit: BoxFit.contain,
),
Container(
margin: EdgeInsets.all(10),
child: Image.asset(
'assets/images/gamora.jpg',
width: 59,
height: 59,
// fit: BoxFit.cover,
),
),
RaisedButton(
child: Text("Set session true"),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10)),
padding: EdgeInsets.all(10),
onPressed: () {
setState(() {
_setSession();
});
},
),
RaisedButton(
child: Text("Go Without session"),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10)),
padding: EdgeInsets.all(10),
onPressed: () {
setState(() {
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (BuildContext context) => HomePage()));
});
},
),
],
),
),
),
);
}
_setSession() async {
await session.set("isLogin", true);
Navigator.pushReplacement(context,
MaterialPageRoute(builder: (BuildContext context) => HomePage()));
}
}
Result Of SplashScreen:
Below is the result of the above SplashScreen.

HomePage.dart
There are two buttons for coming back with setting the session false and another is for setting the session value true. You can go back to the SplashScreen() where you will see the updated session value.
import 'package:flutter/material.dart';
import 'package:flutter_session/flutter_session.dart';
import 'package:flutter_testing_project/SplashScreen.dart';
class HomePage extends StatefulWidget {
const HomePage({Key key}) : super(key: key);
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
var session = FlutterSession();
bool isLogin = false;
@override
void initState() {
// TODO: implement initState
super.initState();
_loadSession();
print("called init");
}
_loadSession() async {
isLogin = await FlutterSession().get("isLogin");
print("_loadSession called = " + isLogin.toString());
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Home Page"),
),
body: Column(
children: [
Container(
// child: Text(isLogin.toString()),
),
RaisedButton(
child: Text("Remove session"),
onPressed: () {
setState(() {
_removeSession();
});
}),
RaisedButton(
child: Text("without Remove session"),
onPressed: () {
setState(() {
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (BuildContext context) => SplashScreen()));
});
})
],
),
);
}
_removeSession() {
FlutterSession().set("isLogin", false);
Navigator.pushReplacement(context,
MaterialPageRoute(builder: (BuildContext context) => SplashScreen()));
}
}
Result:
Below is the result of the above HomePageScreen. From here you can set the session value to false and go to SplashScreen where you will the the updated session value.

Thank You for visiting this tutorial, Hope you learn something from here. For and query please comment in the comment section below. Learn more useful flutter tutorials from here.