title: SwiftUI How can I pass in data into an ObservedObject function
body: <p>I am new to SwiftUI and Swift . I got a Search Bar and a Listview whenever a user types something in the searchbar I do an http request and new data comes in . The issue is that the list is not updating with the new data and I think I know why . <strong>I need to pass my SearchBar response into the ObservedObject variable</strong> . I was reading this <a href="https://stackoverflow.com/questions/58599156/swiftui-observedobject-function-call-in-all-view">swiftui ObservedObject function call in all view</a> however I still didn't find my answer . This is my code</p>
  
  <pre><code>struct RegistrationView: View {
  
      @State private var searchTerm: String = ""
      @State var txt = "" // Txt has the SearchBar text
      @ObservedObject var getData = datas(location: "") // I need to pass it here
  
      var body: some View {
          VStack {
  
              Text("Registration")
              searchView(txt: $txt)
          // datas(location: txt) 
              NavigationView {
                  List(getData.jsonData.filter{ txt == "" ? true : $0.name.localizedCaseInsensitiveContains(txt)}) { i in
                      ListRow(name: i.name,statelong: i.statelong)
  
                  }
  
              }
              .padding(.top, 5.0)
  
          }
  
      }
  }
  
  class datas: ObservableObject
  {
      @Published var jsonData = [datatype]()
  
      init(location: String) {
  
          let session = URLSession(configuration: .default)
          if location == "" {
              return
          }
          let parameter = "location=\(location)"
         if location == "" {
              return
          }
              let url = URL(string:"url")!
              let request = RequestObject(AddToken: true, Url: url, Parameter: parameter)
              session.dataTask(with:request, completionHandler: {(data, response, error) in
              do
              {
                  if data != nil
                  {
                  let fetch = try JSONDecoder().decode([datatype].self, from: data!)
                  DispatchQueue.main.async {
                      self.jsonData = fetch
                      print(fetch) 
                  }
              }
              }
              catch
              {
                  print(error.localizedDescription)
              }
  
  
          }).resume()
          }
  
      }
  </code></pre>
  
  <p>In the above code I want to pass in the <strong>txt</strong> variable into the <strong>getData</strong> variable or do something like this <strong>@ObservedObject var getData = datas(location: txt)</strong> . When the SearchBar is updated then txt gets whatever is inserted into the SearchBar .</p>
  
  <p>If I do something like this</p>
  
  <p>@ObservedObject var getData = datas(location: "Me")</p>
  
  <p>Then the list will update and correctly have everything that starts with <strong>Me</strong> my only issue is getting the SearchBar value inside datas so I don't have to hardcode things . As stated before I need to pass in txt to datas . Any help would be great</p>