Search code examples
kotlinvaadin-flowvaadin-gridvaadin14

Download component in Vaadin grid with Kotlin


I would like to offer a download button in a grid using Kotlin and Vaadin 14:

grid.addComponentColumn{item ->
    val button = Button(item.plandatei)
    val resource = StreamResource(item.plandatei) { this.getStream(item.plandatei) }
    val anchor = Anchor(resource)
    anchor.getElement().setAttribute("download", true)
    anchor.getElement().appendChild(button.getElement())
    return@addComponentColumn anchor
}


fun getStream(filename: String): InputStream {
    val file = File(filename)
    file.readBytes()
}

I receive this error message:

Overload resolution ambiguity. All these functions match.
public constructor StreamResource(name: String!, factory: InputStreamFactory!) defined in 
com.vaadin.flow.server.StreamResource
public constructor StreamResource(name: String!, writer: StreamResourceWriter!) defined in 
com.vaadin.flow.server.StreamResource

How do I turn the InputStream from the getStream function into an InputStreamFactory? Or is it something else?


Solution

  • You need to help the Kotlin compiler a bit there. Please try

    val resource = StreamResource(item.plandatei, InputStreamFactory { this.getStream(item.plandatei) } )