Professional Documents
Culture Documents
How to store login credentials, the right way in Flutter. [2023 Update] | by Carlo Loguercio | System Weakness
Sign up Sign In
You have 2 free member-only stories left this month. Sign up for Medium and get an extra one.
Member-only story
411 6
https://systemweakness.com/how-to-store-login-credentials-the-right-way-in-flutter-857ba6e7e96d 1/23
20/06/2023 10:29 How to store login credentials, the right way in Flutter. [2023 Update] | by Carlo Loguercio | System Weakness
UPDATE 2023
I’ve updated this article to Flutter 3.7, Dart 2.19, and flutter_secure_storage
8.0.0. Check out the repository! ⭐️
https://systemweakness.com/how-to-store-login-credentials-the-right-way-in-flutter-857ba6e7e96d 2/23
20/06/2023 10:29 How to store login credentials, the right way in Flutter. [2023 Update] | by Carlo Loguercio | System Weakness
When we are asked to develop a login screen and there is a “remember me"
check box or an auto-login feature, all we have to do is to save the login
credentials, or some kind of token, into a local storage. So, the next time we
come back into the app, we find the form already filled with our credentials
or, better, an auto-login saves us from the boring login screen.
https://systemweakness.com/how-to-store-login-credentials-the-right-way-in-flutter-857ba6e7e96d 3/23
20/06/2023 10:29 How to store login credentials, the right way in Flutter. [2023 Update] | by Carlo Loguercio | System Weakness
There is a note for Android: this plugin is compatible only with Android SDK
18+ when Keystore was introduced.
Now see how we can use this plugin for a login screen.
https://systemweakness.com/how-to-store-login-credentials-the-right-way-in-flutter-857ba6e7e96d 4/23
20/06/2023 10:29 How to store login credentials, the right way in Flutter. [2023 Update] | by Carlo Loguercio | System Weakness
https://systemweakness.com/how-to-store-login-credentials-the-right-way-in-flutter-857ba6e7e96d 5/23
20/06/2023 10:29 How to store login credentials, the right way in Flutter. [2023 Update] | by Carlo Loguercio | System Weakness
When you fill out the form with credentials and push the Sign In button, you
will store them in secure storage. When you come back in app you will find
your credentials auto-filled in the form.
This is a simple login UI, with any kind of login business logic behind it. I
won’t use any kind of state management, only the old good setState (). It’s only
a simple code sample to show the usage of the package.
First of all, we need to add the package into the pubspec.yaml file.
https://systemweakness.com/how-to-store-login-credentials-the-right-way-in-flutter-857ba6e7e96d 6/23
20/06/2023 10:29 How to store login credentials, the right way in Flutter. [2023 Update] | by Carlo Loguercio | System Weakness
flutter_secure_storage: ^8.0.0
https://systemweakness.com/how-to-store-login-credentials-the-right-way-in-flutter-857ba6e7e96d 7/23
20/06/2023 10:29 How to store login credentials, the right way in Flutter. [2023 Update] | by Carlo Loguercio | System Weakness
As you can see, at line 4, we create a storage that enables us to read and store
data.
At line 7, we read and decrypt a String value identified by the key label, or
null if key is not in the storage. Remember: the key shouldn’t be null.
At line 19, we store and encrypt a value object identified by the key label. If
the key was already in the storage, its associated value is changed. If the
value is null, deletes associated value for the given key .
https://systemweakness.com/how-to-store-login-credentials-the-right-way-in-flutter-857ba6e7e96d 8/23
20/06/2023 10:29 How to store login credentials, the right way in Flutter. [2023 Update] | by Carlo Loguercio | System Weakness
https://systemweakness.com/how-to-store-login-credentials-the-right-way-in-flutter-857ba6e7e96d 9/23
20/06/2023 10:29 How to store login credentials, the right way in Flutter. [2023 Update] | by Carlo Loguercio | System Weakness
The initState() method is called from the framework when the State is created.
(so it’s called only one time, before the UI is draw).
https://systemweakness.com/how-to-store-login-credentials-the-right-way-in-flutter-857ba6e7e96d 10/23
20/06/2023 10:29 How to store login credentials, the right way in Flutter. [2023 Update] | by Carlo Loguercio | System Weakness
Later, when he opens the app again, he will find his credentials already
entered in the form.
https://systemweakness.com/how-to-store-login-credentials-the-right-way-in-flutter-857ba6e7e96d 11/23
20/06/2023 10:29 How to store login credentials, the right way in Flutter. [2023 Update] | by Carlo Loguercio | System Weakness
https://systemweakness.com/how-to-store-login-credentials-the-right-way-in-flutter-857ba6e7e96d 12/23
20/06/2023 10:29 How to store login credentials, the right way in Flutter. [2023 Update] | by Carlo Loguercio | System Weakness
https://systemweakness.com/how-to-store-login-credentials-the-right-way-in-flutter-857ba6e7e96d 13/23
20/06/2023 10:29 How to store login credentials, the right way in Flutter. [2023 Update] | by Carlo Loguercio | System Weakness
You will find the link to the repository on GitHub with the complete code
below.
Congratulations! 🎉
Well done! These simple directions will allow you to add auto-login
functionality to your app, but most importantly, the stored information will
be encrypted in secure storage.
https://systemweakness.com/how-to-store-login-credentials-the-right-way-in-flutter-857ba6e7e96d 14/23
20/06/2023 10:29 How to store login credentials, the right way in Flutter. [2023 Update] | by Carlo Loguercio | System Weakness
Conclusion
What’s next? You should extend this simple sample by storing other user
information, for example the first name, so you can customize the “Welcome
back” message.
An important thing I noted when I upgraded to Flutter 3.7 is that I don’t need
anymore to specify the ToolBar property for the password EditText. In fact,
the right behavior is implemented by default by Flutter itself. So I simply
removed this deprecated code:
TextFormField(
toolbarOptions: const ToolbarOptions(
copy: false,
paste: false,
cut: false,
selectAll: false,
https://systemweakness.com/how-to-store-login-credentials-the-right-way-in-flutter-857ba6e7e96d 15/23
20/06/2023 10:29 How to store login credentials, the right way in Flutter. [2023 Update] | by Carlo Loguercio | System Weakness
),
),
It’s also important to note that the plugin now has full support for all the
platforms: Android, iOS, Windows, Linux, macOS, and Web. Note that for
Web, flutter_secure_storage only works on HTTPS or localhost
environments.
Thank you so much for reading my article. Consider clicking the “clap”
button to show me your interest and to encourage me to go on in my new
adventure ^^
https://systemweakness.com/how-to-store-login-credentials-the-right-way-in-flutter-857ba6e7e96d 16/23
20/06/2023 10:29 How to store login credentials, the right way in Flutter. [2023 Update] | by Carlo Loguercio | System Weakness
If you are interested in other Flutter Security Tips, you can read my other
articles I’ve published here on Medium.
Carlo Loguercio
I’m Carlo and I’m a passionate Italian coder since 2009. I’m Java native
speaker and I’ve never left the JAVA_HOME since I met Dart and Flutter in
late 2019.
Follow me here on Medium to read more articles about Flutter and mobile
development.
https://systemweakness.com/how-to-store-login-credentials-the-right-way-in-flutter-857ba6e7e96d 17/23
20/06/2023 10:29 How to store login credentials, the right way in Flutter. [2023 Update] | by Carlo Loguercio | System Weakness
I’m Carlo and I’m a passionate Italian coder since 2009. I’m Java native speaker and I’ve
never left the JAVA_HOME since I’ve met Dart and Flutter in late 2019.
https://systemweakness.com/how-to-store-login-credentials-the-right-way-in-flutter-857ba6e7e96d 18/23
20/06/2023 10:29 How to store login credentials, the right way in Flutter. [2023 Update] | by Carlo Loguercio | System Weakness
176 2 284 5
https://systemweakness.com/how-to-store-login-credentials-the-right-way-in-flutter-857ba6e7e96d 19/23
20/06/2023 10:29 How to store login credentials, the right way in Flutter. [2023 Update] | by Carlo Loguercio | System Weakness
100 1 113 1
See all from Carlo Loguercio See all from System Weakness
https://systemweakness.com/how-to-store-login-credentials-the-right-way-in-flutter-857ba6e7e96d 20/23
20/06/2023 10:29 How to store login credentials, the right way in Flutter. [2023 Update] | by Carlo Loguercio | System Weakness
289 1 137
Lists
https://systemweakness.com/how-to-store-login-credentials-the-right-way-in-flutter-857ba6e7e96d 21/23
20/06/2023 10:29 How to store login credentials, the right way in Flutter. [2023 Update] | by Carlo Loguercio | System Weakness
708 10 139
36 324 3
https://systemweakness.com/how-to-store-login-credentials-the-right-way-in-flutter-857ba6e7e96d 22/23
20/06/2023 10:29 How to store login credentials, the right way in Flutter. [2023 Update] | by Carlo Loguercio | System Weakness
https://systemweakness.com/how-to-store-login-credentials-the-right-way-in-flutter-857ba6e7e96d 23/23