I have this scenario:
- Perform a post request only when a user taps on a button!
- Get response data
- Navigate to another view passing the data I get from the response
I found similar examples but none was passing arguments from the request's response. The code builds but fails while running with this error:
Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
Also if I remove the argument from the destination like NavigationLink(destination: PostView()) and remove it from the PostView file too, then it works only if I click twice on the button :(. How can we solve this scenario with a better solution?
Here is what I did so far:
// View1
VStack{
NavigationLink(
destination: PostView(post: self.PostViewModel.createdPost), // the error is shown here
isActive: $isActive
){
Button(action: {
if self.PostViewModel.createdPost != nil {
self.isActive = true
} else {
self.PostViewModel.createPost()
}
}) {
Text("Create a Post")
}
}
}
// view2
struct PostView: View {
private var post: Post
init(post: Post){
self.post = post
}
var body: some View {
VStack {
Text("New Post!")
}
}
}