【问题标题】:unable to refresh state after forEach in flutter [duplicate]颤动中的forEach后无法刷新状态[重复]
【发布时间】:2020-12-27 05:24:00
【问题描述】:

我想在以下 forEach 块结束时刷新小部件的状态。但问题是,在 for 循环停止执行之前,我的小部件会自行重建它,即它不会等待它执行然后刷新。

代码:-

publicQuicksDocuments.forEach((element) async{
  if(element.postIdList.isNotEmpty)
  {
    DocumentSnapshot doc=await postsReference.document(element.id).collection('userPosts').document(element.postIdList.last).get();
    element.postIdList.removeLast();
    postList.add(Post.fromDocument(doc));
  }
  else
  {
    publicPostsDocuments.remove(element);
  }
});           //After this for block overs then below code should execute and refresh the state
postList.shuffle();
setState(() {
  loading=false;
});

请阅读上述代码中的注释。

【问题讨论】:

标签: firebase flutter dart google-cloud-firestore


【解决方案1】:

这应该可行,

//awaiting for the future to complete.
var variable=await publicQuicksDocuments.forEach((element) async{
  if(element.postIdList.isNotEmpty)
  {
    DocumentSnapshot doc=await postsReference.document(element.id).collection('userPosts').document(element.postIdList.last).get();
    element.postIdList.removeLast();
    postList.add(Post.fromDocument(doc));
  }
  else
  {
    publicPostsDocuments.remove(element);
  }
}); 
//After this for block overs then below code should execute and refresh the state
postList.shuffle();
setState(() {
  loading=false;
});

【讨论】:

猜你喜欢
  • 2020-10-17
  • 2018-01-26
  • 1970-01-01
  • 1970-01-01
  • 2022-07-07
  • 1970-01-01
  • 1970-01-01
  • 2020-08-04
  • 1970-01-01
相关资源
最近更新 更多