I use Local Storage in my Blazor application, and when opening/closing my browser it saves perfectly. But when I restart my application, my Local Storage suddenly becomes empty. Any ideas?
I tried looking up StackOverflow and Google for possible solutions, but didn't find anything useful sadly... I hope some of you can help
Code to save:
string? storage = await localStorage.GetItemAsync<string>("cart"); List<WarehouseItem> orderesItems = new(); if (storage == null) { orderesItems.Add(new() { Id = warehouseItems[0].Id, WarehouseId = warehouseItems[0].WarehouseId, ItemId = itemId, Quantity = 1 }); string serializedItem = JsonSerializer.Serialize(orderesItems); await localStorage.SetItemAsStringAsync("cart", serializedItem);Code to load:
string? storage = await _localStorage.GetItemAsStringAsync("cart");if (storage != null){ var storedValue = JsonSerializer.Deserialize<List<WarehouseItem>>(storage); CartCount = storedValue.Sum(x => x.Quantity);}(Don't mind the missing } tags, they do get closed)