Blazored / Toast

A JavaScript free toast library for Blazor and Razor Component applications
https://blazored.github.io/Toast/
MIT License
662 stars 92 forks source link

[Bug] BlazoredToast.OnInitializedAsync null reference exception in 4.2.0 #251

Closed rramer-web-com closed 7 months ago

rramer-web-com commented 7 months ago

In 4.2.0, Settings.ShowProgressBar is null in BlazoredToast.OnInitializedAsync when using a ToastService.ShowToast method without explicitly providing optional settings that override ShowProgressBar.

ex. _toastService.ShowToast<Alert>(toastParameters);

This leads to the following runtime error:

Error: System.InvalidOperationException: Nullable object must have a value.
   at System.Nullable`1.get_Value()
   at Blazored.Toast.BlazoredToast.OnInitializedAsync()
   at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()

It looks like the breaking changes were in the following commit which changed ShowProgressBar from bool to bool?

Fix settings to allow instance settings to override global settings (https://github.com/Blazored/Toast/pull/226[)](https://github.com/Blazored/Toast/commit/1f0f1d839ea0177798f6712aff29646fbbacc78a)

BlazoredToasts.BuildCustomToastSettings(Action? settings) might need to define a sensible default.

gracefaz commented 7 months ago

We have been able to recreate this in the repo by using the ShowToast<TComponent> method where ToastSettings is not a parameter. This method sets ToastSettings to null which resulted in ShowProgressBar also being null. According to this line, ShowProgressBar cannot be null.

PR with a proposed fix here: https://github.com/Blazored/Toast/pull/255

hovek commented 1 month ago

Version is 4.2.1 and I have the issue: NullReferenceException: Object reference not set to an instance of an object. Blazored.Toast.BlazoredToast.OnInitializedAsync()

I just have <BlazoredToast /> in MainLayout.razor on the client and it throws on app startup in the browser. If we need to provide ToastSettings it is very inconvenient because it requires a bunch of arguments provided.