![](/img/trans.png)
[英]I want to shuffle the data after getting this data from firestore in flutter
[英]State is not updating. I want to fetch data from the firestore
我已成功从 firestore 检索数据,但无法更新我的 state。请检查代码。
export const TaskContextProvider = (props) => {
const [Tasks, setTasks] = useState([])
const { User } = useContext(AuthContext)
useEffect(() => {
firebase.firestore()
.collection("Users")
.doc(User.id)
.collection("Tasks")
.onSnapshot((snapshot) => {
const newtask = snapshot.docs.map((doc)=>({
id : doc.id,
...doc.data()
}))
setTasks(newtask)
console.log('newtask', newtask)
console.log('Tasks', Tasks)
})
}, [])
下面的控制台output是
newtask (10) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
Tasks []
setTasks
是异步的,当从设置新值的同一 function 中调用时,可能永远不会向您显示正确的值。 相反,在useEffect
之外记录它以查看它是否真的在更新。 然后您将看到每个渲染的 console.log。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.