Problem with localID and preferences

Eduardo Gomez 3,426 Reputation points
2022-01-20T19:34:46.403+00:00

Hello

I have a tiny little problem.

In my App, I have a login method, that set the UserID into preferences

       public async Task LoginAsync(User users) {

            try {
                UserDialogs.Instance.ShowLoading(AppResources.Loading);
                var auth = await AuthProvider.SignInWithEmailAndPasswordAsync(users.Email, users.Password);
                var content = await auth.GetFreshAuthAsync();
                var serializedcontnet = JsonConvert.SerializeObject(content);
                App.UserID = auth.User.LocalId;
                Preferences.Set(App.UserID, App.UserID);
                Preferences.Set(App.FirebaseRefreshToken, serializedcontnet);
                UserDialogs.Instance.HideLoading();
                Application.Current.MainPage = new NavigationPage(new MainPage());
            } catch (FirebaseAuthException ex) {
                Firebasemessages.GetMessages(ex);
            }
            UserDialogs.Instance.HideLoading();

When I start the app again, I do something like

  public App() {

            SyncfusionLicenseProvider.RegisterLicense(KEY);

            InitializeComponent();

            var UserId = Preferences.Get(UserID, string.Empty);

            if (!string.IsNullOrEmpty(UserId)) {
                MainPage = new NavigationPage(new MainPage());
            } else {
                MainPage = new NavigationPage(new LoginPage());
            }
        }

So I am checking if I do not have a user ID, but when I log in, I save the UserID into Preferences, So it shouldn't ask again

Project

DuoNotes

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,348 questions
{count} votes

Accepted answer
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 31,166 Reputation points Microsoft Vendor
    2022-01-21T02:27:01.66+00:00

    Hello,

    Welcome to our Microsoft Q&A platform!

    This Preferences.Set(App.UserID, App.UserID); method means you save a value( App.UserID ) for a given key( App.UserID ) in preferences.

    When you get the UserId ( var UserId = Preferences.Get(UserID, string.Empty); ) , the key is " UserID "( public static string UserID = "UserID"; ) . When you set the UserID into preferences, you set App.UserID = auth.User.LocalId;, the key is the value of auth.User.LocalId not " UserID ", so the value is null.
    You could replace the following code

     App.UserID = auth.User.LocalId;  
    Preferences.Set(App.UserID, App.UserID);  
    

    to

    App.UserID = auth.User.LocalId;  
    Preferences.Set("UserID", App.UserID);  
    

    or to

       // App.UserID = auth.User.LocalId;  
        Preferences.Set(App.UserID, auth.User.LocalId);  
    

    Best Regards,
    Wenyan Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.