Theme flicker at startup

I have the following code in my MainLayout.razor. It causes the theme from the cookie to be loaded. At the start though the default is loaded and shortly after that this code is executed causing a flicker for the theme. How can I solve that?

@code {
	private bool _initialized;

	protected override async Task OnAfterRenderAsync(bool firstRender)
	{
		if (firstRender && !_initialized)
		{
			_initialized = true; // Prevents multiple calls

			// Haal het thema op uit localStorage
			var theme = await JSRuntime.InvokeAsync<string>("localStorage.getItem", "SessyTheme");

			if (!string.IsNullOrEmpty(theme))
			{
				NewTheme = theme;

				// Pas het thema toe
				ThemeService.SetTheme(theme, true);
			}
		}
	}
}

You have to change the theme earlier than OnAfterRenderAsync. More info available here: ThemeService

Yep, works, thanks. You're super fast with your replies, great!