Instance member 'success' of type 'SignUp' cannot be used on instance of nested type 'SignUp.ContentView_Previews'

Code:


//  Login.swift

//  test

//

//  Created by David Csontos on 3/10/21.

//



//

//  ContentView.swift

//  test

//

//  Created by David Csontos on 2/10/21.

//



import SwiftUI

import FirebaseAuth

import FirebaseDatabase



struct SignUp: View {

    

    @State var Value: String = ""

    @State var username: String = ""

    @State var email: String = ""

    @State var password: String = ""

    @State var success = false

    

    var body: some View {

        

        

        NavigationView{

            ZStack {

                

                LinearGradient(gradient: Gradient(colors: [Color.blue, Color.white]), startPoint: .topLeading, endPoint: .bottomTrailing)

                    .ignoresSafeArea()

                

                Rectangle()

                    .fill(Color.white)

                    .frame(width: 300, height: 550)

                    .cornerRadius(10)

                

                

                VStack(spacing: 30){

                    Image(systemName: "bitcoinsign.circle")

                        .font(.system(size: 70, weight: .medium))

                        .padding()

                    TextField("Username", text: $username)

                        .frame(width: 200, height: 30)

                        .padding()

                        .overlay(RoundedRectangle(cornerRadius: 10.0).strokeBorder(Color.blue, style: StrokeStyle(lineWidth: 2.0)))

                        .foregroundColor(.green)

                    TextField("Email", text: $email)

                        .frame(width: 200, height: 30)

                        .padding()

                        .overlay(RoundedRectangle(cornerRadius: 10.0).strokeBorder(Color.blue, style: StrokeStyle(lineWidth: 2.0)))

                        .foregroundColor(.green)

                    TextField("Password", text: $password)

                        .frame(width: 200, height: 30)

                        .padding()

                        .overlay(RoundedRectangle(cornerRadius: 10.0).strokeBorder(Color.blue, style: StrokeStyle(lineWidth: 2.0)))

                        .foregroundColor(.green)

                    Button(action: {

                        createUser(withEmail: email, password: password, username: username)

                        success = true

                

                        

                        }, label: {

                            Text("Sign Up")

                                .frame(width: 200, height: 20, alignment: .center)

                                .padding()

                                .background(Color.blue)

                                .foregroundColor(Color.white)

                                .cornerRadius(15)

                        })

                    

                

                    

                }

            }

        }

            



            

}



struct ContentView_Previews: PreviewProvider {

    

    static var previews: some View {

        if success == false {

            SignUp()

        }

        else{

            Login()

        }

    }

}

}



func createUser(withEmail email: String, password: String, username: String){

    

    Auth.auth().createUser(withEmail: email, password: password) { (result, error) in

        

        if let error = error {

            print("Failed to sign user up", error.localizedDescription)

            return

        }

        

        else{

            guard let uid = result?.user.uid else { return}

            

            let values = ["email": email, "username":username]

            

            Database.database().reference().child(uid).child("users").updateChildValues(values, withCompletionBlock: { (error, ref) in

                

                if let error = error {

                    

                    print("failed to update databse values with error", error.localizedDescription)

                    

                    return

                }

                else {

                    print("user successfully created..")

                        

                }

                

                

                

                

                

            })

        }

        

    }



}

When I try to access the 'success' variable in 'struct ContentView_Previews' it throws the error: Instance member 'success' of type 'SignUp' cannot be used on instance of nested type 'SignUp.ContentView_Previews'. Is there a reason that this is happening/am I missing a simple concept. Any help will be appreciated.

You should use a State var in Preview and a Binding in the View, as described here:

https://stackoverflow.com/questions/60105438/swiftui-preview-provider-with-binding-variables

Note: when you paste code, it is much better too use Paste And Match Style to avoid all the blank lines:

import SwiftUI
import FirebaseAuth
import FirebaseDatabase

struct SignUp: View {
    
    @State var Value: String = ""
    @State var username: String = ""
    @State var email: String = ""
    @State var password: String = ""
    @State var success = false
    
    var body: some View {
        NavigationView{
            ZStack {
                LinearGradient(gradient: Gradient(colors: [Color.blue, Color.white]), startPoint: .topLeading, endPoint: .bottomTrailing)
                    .ignoresSafeArea()
                Rectangle()
                    .fill(Color.white)
                    .frame(width: 300, height: 550)
                    .cornerRadius(10)
                
                VStack(spacing: 30){
                    Image(systemName: "bitcoinsign.circle")
                        .font(.system(size: 70, weight: .medium))
                        .padding()
                    
                    TextField("Username", text: $username)
                        .frame(width: 200, height: 30)
                        .padding()
                        .overlay(RoundedRectangle(cornerRadius: 10.0).strokeBorder(Color.blue, style: StrokeStyle(lineWidth: 2.0)))
                        .foregroundColor(.green)
                    
                    TextField("Email", text: $email)
                        .frame(width: 200, height: 30)
                        .padding()
                        .overlay(RoundedRectangle(cornerRadius: 10.0).strokeBorder(Color.blue, style: StrokeStyle(lineWidth: 2.0)))
                        .foregroundColor(.green)
                    
                    TextField("Password", text: $password)
                        .frame(width: 200, height: 30)
                        .padding()
                        .overlay(RoundedRectangle(cornerRadius: 10.0).strokeBorder(Color.blue, style: StrokeStyle(lineWidth: 2.0)))
                        .foregroundColor(.green)
                    
                    Button(action: {
                        createUser(withEmail: email, password: password, username: username)
                        success = true
                    }, label: {
                        Text("Sign Up")
                            .frame(width: 200, height: 20, alignment: .center)
                            .padding()
                            .background(Color.blue)
                            .foregroundColor(Color.white)
                            .cornerRadius(15)
                    })
                    
                }
                
            }
            
        }
        
    }
    
    
    struct ContentView_Previews: PreviewProvider {
        
        static var previews: some View {
            if success == false {
                SignUp()
            }
            else {
                Login()
            }
        }
        
    }
    
}



func createUser(withEmail email: String, password: String, username: String){
    
    Auth.auth().createUser(withEmail: email, password: password) { (result, error) in
        if let error = error {
            print("Failed to sign user up", error.localizedDescription)
            return
        }
        else {
            guard let uid = result?.user.uid else { return}
            
            let values = ["email": email, "username":username]
            Database.database().reference().child(uid).child("users").updateChildValues(values, withCompletionBlock: { (error, ref) in
                if let error = error {
                    print("failed to update databse values with error", error.localizedDescription)
                    return
                }
                
                else {
                    print("user successfully created..")
                }
            })
            
        }
    }
    
}
Instance member 'success' of type 'SignUp' cannot be used on instance of nested type 'SignUp.ContentView_Previews'
 
 
Q