Reputation: 155
I am new to Flutter and I am writing an app which requires users to login. I already have an API that provides me with a JWT. I then store this using the secure storage library.
Once the user gets the 200 OK with the token, I want to send another request to the server to retrieve the user's details and store it in a kind of singleton class, so I can use it through out the app until he logs out. There are so many buzzwords being thrown at me right now, Providers and BLoCs.
I was wondering what is the best practice to store the current logged in user details in the app? Previously I've been aware of storing the data in a singleton class. But now I'm not sure whether I should write a singleton class or a Provider? (I'm still rusty on my knowledge about how Providers and BLoCs work).
Upvotes: 8
Views: 5054
Reputation: 577
This is how my app stores data:
Create a new dart file for storing "global" variables like so, and store all the "session" data there:
class SessionData{
String encryptedUserId;
String encryptedPassword;
String encryptedToken;
int userId;
}
SessionData globalSessionData;
//Having a clear function is pretty handy
void clearSessionData(){
globalSessionData = new SessionData();
}
And if you named the file for example global.dart
import it, read and write nonpermanent data to it (persist until the app is closed):
import 'global.dart' as global;
global.globalSessionData = ...;
//If you want to clear it:
global.clearSessionData();
If you want to persist the data even if the application closes there are two ways to do that:
Use sharedPrefences to store and retrieve key-data values on the user's device:
import 'package:shared_preferences/shared_preferences.dart';
final SharedPreferences prefs = await SharedPreferences.getInstance();
//Write different types of data
await prefs.setBool("male", true);
await prefs.setDouble("key", 7.0);
await prefs.setString("username", "Mr. User");
//Retrive different types of data
prefs.getBool("male");
prefs.getDouble("key");
prefs.getString("username");
Use sqflite to store advanced data structures, as it is a fully-featured database.
Flutter has a great tutorial on using SQLite with flutter. Although if you go with this type of solution you must know a little SQL.
Upvotes: 0
Reputation: 363
Start Simple
A simple starting point to store the data, which i assume is the form of key/value pairs, would be using shared preferences.
What are Shared Preferences
The main use of Shared Preferences is to save user preferences, settings, maybe data (if not too large) so that next time the application is launched, these pieces of information could be retrieved and used.
How to use
Set an instance variable of Shared Preferences and store the value with a key identifier. Any time you want to retrieve the value when the app starts, just get it using the key. See example below of setting and getting a stored username.
Future<bool> setUserName(String value) async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
return prefs.setString("username",value);
}
Future<String> getUserName(String value) async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
return prefs.getString("username");
}
Installation To installed shared preferences, add this to your package's pubspec.yaml file:
dependencies:
shared_preferences: ^0.5.7+3
More information on the package page.
Upvotes: 3