I have this async function called getRecados() that reaches out to firebase and grab all the data I will need my flatlist to render:
const getRecados = () => {
setRefreshing(true)
try {
const recadosRef = firebase.firestore().collection('recados');
recadosRef.orderBy('timestamp', 'desc').get().then(snapshot => {
const recadosList = new Array()
// Does some filtering on the data it just grabbed from firebase
if (user_setor.length == 0) {
snapshot.forEach(doc => {
if ((doc.data().curso_dest == user_curso
|| doc.data().curso_dest == '')
&& (doc.data().ano_dest == user_ano
|| doc.data().ano_dest == '')
&& (doc.data().turno_dest == user_turno
|| doc.data().turno_dest == ''))
recadosList.push(doc)
})
} else {
snapshot.forEach(doc => {
if (user_setor.includes(doc.data().remetente)) {
recadosList.push(doc)
}
})
}
// fullData is an useState
setFullData(recadosList.map((doc) => ({
id: doc.id,
titulo: doc.data().titulo,
timestamp: doc.data().timestamp,
remetente: doc.data().remetente,
curso_dest: doc.data().curso_dest,
ano_dest: doc.data().ano_dest,
turno_dest: doc.data().turno_dest,
texto: doc.data().texto
})))
setLoading(false)
setRecados(fullData)
})
} catch (err) {
Alert.alert("Erro ao consultar os recados!!!", err.message);
}
setRefreshing(false)
};
And I call it as soon as the homeScreen renders with:
useEffect(() => {
getRecados()
}, []);
But when I open the homeScreen for the first time the flatList is empty, but when I execute getRecados() again with a button the flatList works fine and displays the data from firebase, so that made me conclude that the flatList is rendering before getRecados() can set fullData's value for the first time, so how do I make it wait for getRecados to finish whatever its doing?
Here's the flatlist code:
<FlatList
data={recados}
onRefresh={getRecados}
refreshing={refreshing}
showsVerticalScrollIndicator={false}
renderItem={({ item }) => (
<Card
texto={item.texto}
titulo={item.titulo}
timestamp={item.timestamp}
info1={item.remetente}
info2={
"ao " + item.ano_dest + "º " + item.curso_dest + " " + item.turno_dest
}
/>
)}
/>;