Cannot declare local wrapped variable in result builder

I'm trying to change the view without using NavigationLink because it has the bar on top

See this: https://developer.apple.com/forums/thread/667742 This is my code:

struct ContentView: View {
  var body: some View {
    @State var another = false
    if (another) {
      AnotherView()
    } else {
      ContentView()
    }
  }

I get an error: Cannot declare local wrapped variable in result builder.

@State var another = false
struct ContentView: View {
  var body: some View {
    if (another) {
      AnotherView()
    } else {
      ContentView()
    }
  }

Now: Property wrappers are not yet supported in top-level code. Where do i put the variable and the logic?

Answered by darkpaw in 726880022

You put it inside the struct but not inside var body: some View:

struct ContentView: View {
  @State var another = false

  var body: some View {
    if(another) {
      AnotherView()

    } else {
      ContentView()
    }
  }
}
Accepted Answer

You put it inside the struct but not inside var body: some View:

struct ContentView: View {
  @State var another = false

  var body: some View {
    if(another) {
      AnotherView()

    } else {
      ContentView()
    }
  }
}
Cannot declare local wrapped variable in result builder
 
 
Q