0

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

2
  • 1
    Could be a result of the redundant ScollView. List already scrolls — shouldn’t be inside another scrolling view. Commented Mar 22, 2021 at 20:47
  • That fixed the problem!! Thankyou! Commented Mar 22, 2021 at 20:56

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.