chrispsv
chrispsv

Reputation: 435

swiftui list doens't appear but array isn't empty

I am working on a Swiftui file that loads data from Firebase. It did work but when I added things it suddenly stopt working... I tried to strip it back down but I can't get it working again. Does anyone know what I do wrong?

import SwiftUI
import Firebase

struct Fav: View {
@StateObject var loader = Loader()

var body: some View {
    ScrollView {
        if loader.userfav.count != 0 {
            List (loader.userfav, id: \.id) { fav in
                Text(fav.name.capitalized)
            }
        }
        else
        {
            Text("You haven't added favorits yet...")
        }
    }
    .onAppear{
        loader.loadfav(loadfavorits: "asd")
    }
    .navigationBarTitle("")
    .navigationBarHidden(true)
    .navigationBarBackButtonHidden(true)
}
func deletefav (docid: String) {
    print(docid)
}
}
struct Fav_Previews: PreviewProvider {
    static var previews: some View {
        Fav()
    }
 }

and the loader file

import Foundation
import Firebase
import FirebaseFirestore


class Loader : ObservableObject {
     private var db = Firestore.firestore()
     @Published var userfav = [fav]()

        func loadfav (loadfavorits: String) {
    userfav = [fav]()
    
    db.collection("favo").whereField("user", isEqualTo: loadfavorits).getDocuments() { (querySnapshot, err) in
        if let err = err {
            print("Error getting favorits: \(err.localizedDescription)")
        }
        else
        {
            for fav in querySnapshot!.documents {
                let brand = fav.get("brand") as! String
                let store = fav.get("store") as! String
                let name = fav.get("name") as! String
                let type = fav.get("type") as! String
                let docid = fav.get("docid") as! String
            
                self.userfav.append(fav(brand: brand, store: store, name: name, type: type, docid: docid))
            }
        }
    }
}
}

It doesn't show the Text("You haven't added favorits yet...") So that means dat loader.userfav.count is not empty

Upvotes: 0

Views: 60

Answers (1)

jnpdx
jnpdx

Reputation: 52387

Having a List embedded in a ScrollView (which also scrolls) can lead to layout problems. Remove the outer ScrollView and the issue will be solved.

Upvotes: 2

Related Questions