0

I want to have a mother view that displays a different child view based on a @State bool. However I get two errors when doing that.

On the start of the body closure:

Struct 'ViewBuilder' requires that 'EmptyCommands' conform to 'View'

Return type of property 'body' requires that 'EmptyCommands' conform to 'View'

And inside the control flow statement:

Closure containing control flow statement cannot be used with result builder 'CommandsBuilder'

struct ResultView: View {
    @State var resultViewSuccess = false

    let resultViewModel: ResultViewModel

    var body: some View {
        Group {
            if let showresultView = resultViewSuccess {
                ViewOne(viewModel: resultViewModel)
            } else {
                ViewTwo(
                    resultViewSuccess: $resultViewSuccess,
                    viewModel: resultViewModel,
                )
            }
        }
    }
}

struct ViewTwo: View {
    @Binding var resultViewSuccess: Bool
    @StateObject var viewModel: ResultViewModel

    var body: some View {
        NavigationView {
            ButtonResult(
                resultViewSuccess:       $resultViewSuccess, 
                viewModel: viewModel)
        }               
    }


struct ButtonResult: View {
    @Binding var resultViewSuccess: Bool
    @StateObject var viewModel: ResultViewModel

    var body: some View {
        Button(action: {
            self.resultViewSuccess = true
        }) {
            Text("View Results")
        }
    }
}
  • Looking at the attached code, I don't see anything wrong. Maybe show us `ViewOne` & `ViewTwo` for more details. – Timmy Jul 25 '22 at 09:39
  • Added the code for ViewTwo – Jonas Bäumer Jul 25 '22 at 09:49
  • Based on your code you are sending a true value to first view which it’s extra and not need to do it! Because in that case, it would be always true! Also i noticed you are using if let for non-optinal! That’s is also extra! – ios coder Jul 25 '22 at 10:38
  • 1
    So where exactly is `EmptyCommands` in your provided code? Also, this `if let showresultView = resultViewSuccess` should be `if resultViewSuccess` because otherwise the first condition will always be met. – Steven-Carrot Jul 25 '22 at 12:39
  • It is not, I think it is some internal class SwiftUI uses – Jonas Bäumer Jul 25 '22 at 14:23

0 Answers0