Get All Documents From Collection Firestore Android Kotlin Code Example


Example 1: get one document based on id in firestore


//retrieve one document and save it to userDetails
const [userDetails, setUserDetails] = useState('')
db.collection('users').doc(id).get()
.then(snapshot => setUserDetails(snapshot.data()))

Example 2: get data from firestore


const [hospitalsDetails, setHospitalsDetails] = useState([])
useEffect(()=>{
//load hospitals into hospitalsList
const hospitals = []
db.collection('Hospitals').get()
.then(snapshot => {
snapshot.docs.forEach(hospital => {
let currentID = hospital.id
let appObj = { ...hospital.data(), ['id']: currentID }
hospitals.push(appObj)

hospitals.push(hospital.data())
})
setHospitalsDetails(hospitals)
})
},[])

Comments

Popular posts from this blog

Converting A String To Int In Groovy

"Cannot Create Cache Directory /home//.composer/cache/repo/https---packagist.org/, Or Directory Is Not Writable. Proceeding Without Cache"

Android How Can I Convert A String To A Editable