title: How to setup NavigationLink inside SwiftUI list
body: <p>I am attempting to set up a SwiftUI weather app. when the user searches for a city name in the textfield then taps the search button, a NavigationLink list item should appear in the list. Then, the user should be able to click the navigation link and re-direct to a detail view. My goal is to have the searched navigation links to populate a list. However, my search cities are not populating in the list, and I'm not sure why. In ContentView, I setup a list with a ForEach function that passes in cityNameList, which is an instance of the WeatherViewModel. My expectation is that Text(city.title) should display as a NavigationLink list item. How should I configure the ContentView or ViewModel to populate the the list with NavigationLink list items? See My code below:</p>
  <p>ContentView</p>
  <pre><code>import SwiftUI
  
  struct ContentView: View {
      
      // Whenever something in the viewmodel changes, the content view will know to update the UI related elements
      @StateObject var viewModel = WeatherViewModel()
      @State private var cityName = &quot;&quot;
  
      var body: some View {
          NavigationView {
  
              VStack {
                  TextField(&quot;Enter City Name&quot;, text: $cityName).textFieldStyle(.roundedBorder)
                  
                  Button(action: {
                      viewModel.fetchWeather(for: cityName)
                      cityName = &quot;&quot;
                  }, label: {
                      Text(&quot;Search&quot;)
                          .padding(10)
                          .background(Color.green)
                          .foregroundColor(Color.white)
                          .cornerRadius(10)
                  })
                  
                  List {
                      ForEach(viewModel.cityWeather, id: \.id) { city in
                          NavigationLink(destination: DetailView(detail: viewModel)) {
                              HStack {
                                  Text(city.cityWeather.name)
                                      .font(.system(size: 32))
                              }
                          }
                      }
                  }
                  
                  Spacer()
              }
              .navigationTitle(&quot;Weather MVVM&quot;)
          }.padding()
      }
  }
  
  struct ContentView_Previews: PreviewProvider {
      static var previews: some View {
          ContentView()
      }
  }
  </code></pre>
  <p>ViewModel</p>
  <pre><code>import Foundation
  
  class WeatherViewModel: ObservableObject {
      
      //everytime these properties are updated, any view holding onto an instance of this viewModel will go ahead and updated the respective UI
          
      @Published var cityWeather: WeatherModel = WeatherModel()
      
      func fetchWeather(for cityName: String) {
  
          guard let url = URL(string: &quot;https://api.openweathermap.org/data/2.5/weather?q=\(cityName)&amp;units=imperial&amp;appid=&lt;MyAPIKey&gt;&quot;) else {
              return
          }
          
          let task = URLSession.shared.dataTask(with: url) { data, _, error in
              // get data
              guard let data = data, error == nil else {
                  return
              }
              
              //convert data to model
              do {
                  let model = try JSONDecoder().decode(WeatherModel.self, from: data)
                  
                  DispatchQueue.main.async {
                      self.cityWeather = model
                  }
              }
              catch {
                  print(error)
              }
          }
          task.resume()
      }
  }
  </code></pre>
  <p>Model</p>
  <pre><code>import Foundation
  
  struct WeatherModel: Identifiable, Codable {
      var id = UUID()
      var name: String = &quot;&quot;
      var main: CurrentWeather = CurrentWeather()
      var weather: [WeatherInfo] = []
      
      func firstWeatherInfo() -&gt; String {
          return weather.count &gt; 0 ? weather[0].description : &quot;&quot;
      }
  }
  
  struct CurrentWeather: Codable {
      var temp: Float = 0.0
  }
  
  struct WeatherInfo: Codable {
      var description: String = &quot;&quot;
  }
  
  </code></pre>
  <p>DetailView</p>
  <pre><code>import SwiftUI
  
  struct DetailView: View {
      
      var detail: WeatherViewModel
      
      var body: some View {
          
          VStack(spacing: 20) {
              Text(detail.cityWeather.name)
                  .font(.system(size: 32))
              Text(&quot;\(detail.cityWeather.main.temp)&quot;)
                  .font(.system(size: 44))
              Text(detail.cityWeather.firstWeatherInfo())
                  .font(.system(size: 24))
          }
          
  
      }
  }
  
  struct DetailView_Previews: PreviewProvider {
      static var previews: some View {
          DetailView(detail: WeatherViewModel.init())
      }
  }
  </code></pre>