microsoft / dotnet-podcasts

.NET reference application shown at .NET Conf featuring ASP.NET Core, Blazor, .NET MAUI, Microservices, Orleans, Playwright, and more!
https://dot.net
MIT License
2.75k stars 745 forks source link

Issue with initial get of Theme preference #150

Closed davefxy closed 2 years ago

davefxy commented 2 years ago

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.

jamesmontemagno commented 2 years ago

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.

jamesmontemagno commented 2 years ago

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

victorperez2911 commented 2 years ago

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

jamesmontemagno commented 2 years ago

Yup, i jsut saw this as well, fixing up