List and MysqlDB

Hello everyone, I just started creating my first app for iOS. I have a question for you. I am creating a List with results taken from a query on my personal database. Now I would like to pass the "f" parameter containing a date to the entire query. Would anyone know how to help me?

The error is: " cannot use instance member "f" within property initializer "

Code Block
import SwiftUI
struct BarberView: View {
    @Binding var f:String
    
    
    
    
    var fetcher = MovieFetcher(f: f)
    var body: some View {
        
        VStack{
            Text("\(f)")
            
           
            List(fetcher.movies ) { movie in
                                VStack (alignment: .leading) {
                                    Text(movie.data)
                                    Text(movie.ora)
                                        .font(.system(size: 11))
                                        .foregroundColor(Color.gray)
                                    Text(movie.barber)
                                    Text(movie.user)
                                }
                            }
            
            
        }
      
        
    }
}
public class MovieFetcher: ObservableObject {
    @Published var movies = [Movie]()
   
    @Published var f:String
    
    
    init(f: String){
        self.f = f
        load(f: f)
    }
    
    
    
    func load(f: String) {
        let url = NSMutableURLRequest(url: NSURL(string: "http://familyverde.ddns.net/prova/checkapp.php")! as URL)
        
        
        url.httpMethod = "POST"
        
        let postString = "data=\(f)"
        url.httpBody = postString.data(using: String.Encoding.utf8)
        
        
        print("IL VALORE DI F: \(f)")
       
    
        URLSession.shared.dataTask(with: url as URLRequest) {(data,response,error) in
            do {
                if let d = data {
                    let decodedLists = try JSONDecoder().decode([Movie].self, from: d)
                    DispatchQueue.main.async {
                        self.movies = decodedLists
                    }
                }else {
                    print("No Data")
                }
            } catch {
                print ("Error")
            }
            
            
        }.resume()
         
    }
}
struct Movie: Codable, Identifiable {
    public var id: String
    public var data: String
    public var ora: String
    public var barber: String
    public var user: String
    
    enum CodingKeys: String, CodingKey {
           case id = "id"
           case data = "data"
           case ora = "ora"
        case barber = "barber"
        case user = "user"
        }
}






The error is: " cannot use instance member "f" within property initializer "

As the error message is stating, you cannot use f (@Binding var in BarberView) in the initial value of fetcher.

How to fix depends on how you are using BarberView and how you want to pass the value of f.
Can you explain these?
I have a HomeUser to which I apply an "if" ... "if email == email@email.it" then I call BarberView passing as argument "f" ... BarberView will have to show me a List containing the lines of the result of the query shown above

I have a HomeUser to which I apply an "if" ... "if email == email@email.it" then I call BarberView passing as argument "f" ... BarberView will have to show me a List containing the lines of the result of the query shown above

Thanks for your reply, and sorry I was not clear enough.

Can you show the code you call BarberView passing as argument "f"?
Especially, please include enough code to clarify what is passed to f, it should be an @State var, and whether it is changed or not while BarberView is shown.
I have a View called "HomeUser" in which I have an "if statement" that checks the email ... if the email is equal to "email@email.com" then call barberview passing f as parameter.

HomeUser.swift:

Code Block
import SwiftUI
struct HomeUser: View {
    @State var barberid:Int = 0
    @Binding var email: String
    @State var flagView = false
    var dateFormatter: DateFormatter {
            let formatter = DateFormatter()
        formatter.dateFormat = "dd\\\\MM\\\\yyyy"
        formatter.locale = Locale(identifier: "it_IT")
            return formatter
        }
        @State private var date = Date()
    @State var valore:String = ""
    
    
    @State public var f = ""
    
    
    var body: some View {
        NavigationView{
            VStack{
                Spacer()
                
                if email == "Marcomeola@gmail.com" {
                    
                    DatePicker("SELEZIONA LA DATA", selection: $date, displayedComponents: .date)
                                    .datePickerStyle(GraphicalDatePickerStyle())
                                    .frame(maxHeight: 400)
                    
                    var formatter = dateFormatter.string(from: date)
                    
                                        
                    NavigationLink(
                        destination: BarberView(f: $f), isActive: $flagView,
                        label:{
                            Button(action: {
                                f = formatter
                                
                                flagView = true
                            }, label: {
                                Text("MOSTRA")
                            })
                        })
                    
                    
                    
                    
                    
                }else{
                    NavigationLink(
                        destination: PrenotazioneView(barberid: $barberid, email:$email), isActive: $flagView,
                        label:{
                            Button(action: {
                                barberid = 1
                                print("hai cliccato marco ID: \(barberid)")
                                flagView = true
                            }, label: {
                                Image("card_marco")
                                    .resizable()
                                    .frame(width: 400.0, height: 250)
                            })
                        })
                    NavigationLink(
                        destination: PrenotazioneView(barberid: $barberid, email:$email), isActive: $flagView,
                        label:{
                            Button(action: {
                                barberid = 2
                                print("hai cliccato peppe ID: \(barberid)")
                                flagView = true
                            }, label: {
                                Image("card_peppe")
                                    .resizable()
                                    .frame(width: 400.0, height: 250)
                            })
                        })
                }
                Spacer()
            }.background(
                Image("sfondo")
                    .resizable()
                    .aspectRatio(contentMode: /*@START_MENU_TOKEN@*/.fill/*@END_MENU_TOKEN@*/)
                    .edgesIgnoringSafeArea(/*@START_MENU_TOKEN@*/.all/*@END_MENU_TOKEN@*/)
                )
        }.navigationBarHidden(true)
    }
}
func showappointment(format:String) -> String{
    // inizializzo semaforo
    let semaphore = DispatchSemaphore(value: 0)
    var responseString = ""
           let request = NSMutableURLRequest(url: NSURL(string: "http://familyverde.ddns.net/prova/showappoointment.php")! as URL)
           request.httpMethod = "POST"
           
           let postString = "data=\(format)"
           request.httpBody = postString.data(using: String.Encoding.utf8)
    
           let task = URLSession.shared.dataTask(with: request as URLRequest) {
               data, response, error in
               
               if error != nil {
                   print("error=\(error)")
                
                   return
               }else{
                print("response = \(response)")
                
                responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue) as! String
                print("responseString = \(responseString)")
                
                
               
                
                // tolgo semaforo
                semaphore.signal()
                
                
               }
           }
           task.resume()
    
    // attivo semaforo
    _ = semaphore.wait(timeout: .distantFuture)
    print("VALORE: \(responseString)")
    return responseString
}

then call barberview passing f as parameter.

Thanks for showing your code. Seems your BarberView is only presented as a destination of NavigationLink.

Please try the following.

First, add an initializer which does not call load(f:) to MovieFetcher:
Code Block
public class MovieFetcher: ObservableObject {
@Published var movies = [Movie]()
//↓ Add an initializer which does not call `load(f:)`
init() {}
// @Published var f:String
//
//
// init(f: String){
// self.f = f
// load(f: f)
// }
//...
}


And then change your BarberView to use it, and call load(f:) explicitly:
Code Block
struct BarberView: View {
@Binding var f:String
@StateObject var fetcher = MovieFetcher() //<-
var body: some View {
VStack {
//...
}
//↓ Call `load(f:)` explicitly
.onAppear {
fetcher.load(f: f)
}
}
}


Assuming MovieFetcher works as expected.
List and MysqlDB
 
 
Q