Using Flutter_Secure_Storage setting last one seems to override all

  1. I am trying to store username, email and ID

It seems ID is being stored but username and email aren’t, they seem to be overridden with the ID?!

Here is my UserSecureStorage class of getters and setters

import 'package:flutter_secure_storage/flutter_secure_storage.dart';

class UserSecureStorage {
  static final _storage = FlutterSecureStorage();
  static const _keyUsername = '';
  static const _keyUserId = '';
  static const _keyUserEmail = '';

// get set profilename
  static Future setProfileName(String profileName) async =>
      await _storage.write(key: _keyUsername, value: profileName);

  static Future<String> getProfileName() async =>
      await _storage.read(key: _keyUsername);

// get set profileemail
  static Future setProfileEmail(String profileEmail) async =>
      await _storage.write(key: _keyUserEmail, value: profileEmail);

  static Future<String> getProfileEmail() async =>
      await _storage.read(key: _keyUserEmail);

// get set profileid
  static Future setProfileId(String profileId) async =>
      await _storage.write(key: _keyUserId, value: profileId);

  static Future<String> getProfileId() async =>
      await _storage.read(key: _keyUserId);
}

here is where I am getting the profile details form the API and then attempting to set, Username, email, ID

apiService
                                            .loadProfile()
                                            .then((profile) async {
                                          await UserSecureStorage
                                              .setProfileName(profile.name);
                                          await UserSecureStorage.setProfileEmail(
                                              profile.email);
                                          await UserSecureStorage.setProfileId(
                                              profile.id);
});

here is where I am getting them:

Future init() async {
    final profilename = await UserSecureStorage.getProfileName() ?? '';
    final profileemail = await UserSecureStorage.getProfileEmail() ?? '';
    final profileid = await UserSecureStorage.getProfileId() ?? '';
    setState(() {
      profileName = profilename;
      profileEmail = profileemail;
      profileId = profileid;
    });
  }

and a bit further down:

new UserAccountsDrawerHeader(
          accountName: new Text('$profileName'),
          accountEmail: new Text('$profileEmail'),.......

as you can see, I am not even trying to output ID and they both equal 2 (the ID of the person logged in)

Hello and welcome;

Just a guess: it seems to me that your keys are all equals to the same empty string, so

_storage.write(key: _keyUsername, value: profileName);

_storage.write(key: _keyUserEmail, value: profileEmail);

_storage.write(key: _keyUserId, value: profileId);

they are all writing the same key, and profileId is the last value you are left with.

you’d want to name the three keys differently than empty strings.

1 Like

Thank you thank you!