Hi Olli, thanks for the add-on. It works, but the first time, I click the

Hi Olli,

thanks for the add-on. It works, but the first time, I click the download button, nothing is downloaded. From the second click it works perfectly.
Here is the code:

Button downloadDataButton = new Button("Download data");
downloadDataButton.addThemeVariants(ButtonVariant.LUMO_PRIMARY);
downloadDataButton.addClickListener(event -> {
			 FileDownloadWrapper buttonWrapper = new FileDownloadWrapper(
					new StreamResource("ResultDataFor" + queryName + ".xlsx", () -> {
						
							return new ByteArrayInputStream(service.createDataDownloadFile(queryName));
						
					})
				);
			buttonWrapper.wrapComponent(downloadDataButton);
			dataDownloadLayout.add(buttonWrapper);
			
		});	
		dataDownloadLayout.add(info, downloadDataButton);

I debugged the code and can see that the first time I press the button, nothing is returned, but without error. It seems the code is ignored. Maybe the initialization of the FileDownloadwrapper is wrong? What is wrong?

You are adding the FileDownloadWrapper to dataDownloadLayout in the Button’s click listener, which means that it’s not there when you click the Button for the first time. Initialize the FileDownloadWrapper before that (outside the click listener) and it should work. Note that you won’t need to add the button to the layout, just add the wrapper.

Thank you! It works now correctly!