swiftuiswiftui-navigationstack

Fail to show bio in DetailView while using NavigationStack in SwiftUI


I am learning iOS development using SwiftUI. I pass my user to DetailView using NavigationStack :

let shape = RoundedRectangle(cornerRadius: Constants.cornerRadius)
            NavigationStack {
                ScrollView {
                    LazyVGrid(columns: [GridItem(.adaptive(minimum: Constants.gridItemSize))]) {
                        ForEach(userWrappers) { userWrapper in
                            Section(userWrapper.section) {
                                ForEach(userWrapper.users, id: \.self.login) { user in
                                    NavigationLink(value: user)  {
                                        VStack {
                                            AsyncImage(url: URL(string: user.avatarUrl)) { image in
                                                image
                                                    .resizable()
                                                    .scaledToFit()
                                                    .frame(width: Constants.frameSize, height: Constants.frameSize)
                                                    .clipShape(shape)
                                            } placeholder: {
                                                shape.foregroundColor(.secondary)
                                                    .frame(width: Constants.frameSize, height: Constants.frameSize)
                                            }
                                            Text(user.login)
                                                .font(.title3)
                                        }.padding()
                                    }
                                }
                            }
                        }
                    }.navigationDestination(for: GithubUser.self) { user in
                        DetailView(user: user)
                    }
                    .navigationTitle("List")
                }
            }
        }

As you see I pass user to DetailView. It shows user name and avatar, but it fails to show bio always in DetailView, and display No Bio as you see in the bellow code :

VStack {
            AsyncImage(url: URL(string: user.avatarUrl)) { image in
                image
                    .resizable()
                    .scaledToFit()
            } placeholder: {
                Rectangle()
                    .foregroundColor(.secondary)
                    .frame(width: Constants.frameSize, height: Constants.frameSize)
            }.padding()
            Text(user.bio ?? "No Bio")
                .padding()
            Spacer()
        }
        .navigationTitle(user.login)

Here is my model class :

struct GithubUser: Codable, Hashable {
    let login: String
    let avatarUrl: String
    let bio: String?
}

You can find source code at https://github.com/alirezaeiii/Networking-Assignment

Can you help me out?


Solution

    1. As you might see from Safari or Postman, the Github users API does not contain a bio field https://api.github.com/users/alirezaeiii/following

    2. According to Github REST API documentation, you can get the bio field by making another call to get user data. For example:

      https://api.github.com/user/69767

    So, you will need to perform another API call for each user for display it in the details screen (the data model that you get from the list and from the details are different).

    good luck