microsoft/dotnet-podcasts

Issue with initial get of Theme preference

davefxy opened this issue · 4 comments

I don't know if I am using this app properly. on my PC running Windows 11, I am compiling and running only the Mobile app. When I run the Mobile app it immediately errors out with a null reference because the Theme preference has never been set;
The line in error is in Settings.cs :
get => Enum.Parse<AppTheme>(Preferences.Get(nameof(Theme), Enum.GetName(theme)));
Since the Theme preference has never been set, the code gets a null reference.
To get this app to run, I modified the code as follows:

     get {
           if (!Preferences.ContainsKey(nameof(Settings.Theme)))
                Preferences.Default.Set(nameof(Settings.Theme), "Light");
            return Enum.Parse<AppTheme>(Preferences.Get(nameof(Theme), Enum.GetName(theme)));
        }

This is done only to demonstrate I determined a possible reason for the error.

So, this should work fine and i haven't seen issues on my end because the Preferences.Get will return the default if it isn't set.... so it would return "Light" automatically.

I put in Light so that should fix it up any mroe. let me know.

Hi

here the error still occurs if I start the project as "Windows Machine" as Target, but it does not occur if I use the Android target (Emulator)

image

Yup, i jsut saw this as well, fixing up