I've created a list using LazyVStack
and inside of the list there are ListItemView
s.
What I'm trying to do is, when a ListItemView
gets tapped, I want to make an async call depending on the selected item, and then navigate to the view with the data I fetched.
I've tried to do so by placing a Task
inside of destination
parameter of NavigationLink
but I think this is not permissible. I get Type '() -> Task<some View, Never>' cannot conform to 'View'
error.
Here is the list that I'm implementing
ScrollView {
LazyVStack {
ForEach(items) { item in
NavigationLink(destination: {
Task {
await detailView(from: item.id) // <- Type '() -> Task<some View, Never>' cannot conform to 'View'
}
}, label: {
ListItemView(item: item)
})
.padding(.horizontal, 8)
} //: ForEach
} //: LazyVStack
} //: ScrollView
And here is the method that I use to generate destination view.
private func detailView(from id: Int) async -> some View {
guard let product = await viewModel.getProduct(with: id) else { return Text("Error").eraseToAnyView() }
selectedProduct = product
return ProductDetailView(product: $selectedProduct).eraseToAnyView()
}
Any ideas how can I implement this?