UI navigation

Hi Vaadins,
it seems there are different solutions to navigate programmatically via UI.navigate. But what is the difference between the two different versions? One preferred over the other? Thanks for some explaining words.

        grid.asSingleSelect().addValueChangeListener(event ->
                UI.getCurrent().navigate(FlightDetailsView.class, new RouteParam("flight-id", event.getValue().getFlightId()))
        );

        grid.asSingleSelect().addValueChangeListener(event ->
                grid.getUI().ifPresent(ui ->
                        ui.navigate(FlightDetailsView.class, new RouteParam("flight-id", event.getValue().getFlightId()))
                )
        );

They all end up calling the same method. Choose what is better for your code.

Or are you talking about how to get the UI?

No exactly. Then I use the optional-free, static version. Thanks!

Use UI.getCurrent() if you are sure the code is executed in a Vaadin request

this is usually true for component listener, unless you are doing updates in a background thread without using UI.access() (in this case you should always store a UI reference and execute the update action in an UI.access call)

They all end up calling the same method. Choose what is better for your code.
Just for clarity, I was talking about the navigate method here, not about getting the UI instance

Thanks @versatile-zorse for clarification. Now I got it. :slightly_smiling_face:

How about this enhancement :thinking: ComponentEvent should expose UI · Issue #18818 · vaadin/flow · GitHub