Data between views

Hi I am writing a small app mainly for practice and am having difficulty JSON data retrieved from a remote server between views.

Using the code below I am able to retrieve my data from the server decode it etc and make a list -

import SwiftUI

import Combine

import Foundation

struct FlightView: View {

    @ObservedObject var fetcher = FlightFetcher()

    
    var body: some View {

        NavigationStack{

            VStack {

                List(fetcher.flights) { flight in

                    NavigationLink(destination: DetailView(flightDetail: Flight)) {

                        Text(flight.station_code)

                        Text(flight.fltno)

                    }

                }

            }

        }

    }

}



public class FlightFetcher: ObservableObject {

@Published var flights  =   [Flight]()

    init(){

        load()

    }

    func load() {

        let url = URL(string: "my address")!

    
        URLSession.shared.dataTask(with: url) {(data,response,error) in

            do {

                if let d = data {

                    let decodedLists = try JSONDecoder().decode([Flight].self, from: d)

                    DispatchQueue.main.async {

                        self.flights = decodedLists

                    }

                }else {

                    print("No Data")

                }

            } catch {

                print ("Error")

            }
 
        }.resume()

    }

}

struct Flight: Hashable, Identifiable, Codable {

    var id: Int

    var station_code: String

    var ac_type: String

    var fltno: String

    var delay_incurred: String

    var nightstop: String

    var delay_mins: Int

    var date: String

    var delay_defect: String

    var std: String

    var atd: String

    var delay_count: Int

    var regn : String

    var call_type : String

    var movement_ind : Int

}

Okay, so that fetched the data and makes the list as we'd expect. The difficulty comes when trying to use the navigation link to take me to a detailed view. I cannot seem to be able to pass the data to the DetailView view.

import SwiftUI

import Combine

import Foundation

struct DetailView: View {

   // @EnvironmentObject var flights: FlightFetcher

    var flightDetail: Flight

    var body: some View {

        VStack{

            Text("Flight Details:")

            Text(flightDetail.fltno)

        }

    }

}

I have tried various methods, including @EnvironmentObject (commented out above) to pass the data from the FlightView to the DetailView so that the other parameters of the Flight struct can be shown in the view (ac_type, date etc...) I'm not sure where I'm going wrong on this one.

Answered by Claude31 in 718742022

You have probably a typo:

                List(fetcher.flights) { flight in
                    NavigationLink(destination: DetailView(flightDetail: Flight)) {

Flight is the class, not the instance you pass as parameter in the closure.

replace Flight with flight as flightDetail parameter:

                List(fetcher.flights) { flight in
                    NavigationLink(destination: DetailView(flightDetail: flight)) {

Should have added - the error I currently get with the code show above is "Cannot convert value of type 'Flight.Type' to expected argument type 'Flight'" on the Navigation link of the FlightView.

If trying to use the EnvironmentObject I tend to get error "Trailing closure passed to parameter of type 'HorizontalAlignment' that does not accept a closure"

Accepted Answer

You have probably a typo:

                List(fetcher.flights) { flight in
                    NavigationLink(destination: DetailView(flightDetail: Flight)) {

Flight is the class, not the instance you pass as parameter in the closure.

replace Flight with flight as flightDetail parameter:

                List(fetcher.flights) { flight in
                    NavigationLink(destination: DetailView(flightDetail: flight)) {
Data between views
 
 
Q