【问题标题】:Best practice for calling two different endpoints for a widget?为小部件调用两个不同端点的最佳实践?
【发布时间】:2018-05-15 03:02:06
【问题描述】:

当需要两个不同的端点来渲染某些东西时,什么被认为是最佳实践?现在我正在嵌套 FutureBuilders,但这似乎并不理想。

例子:

@override
Widget build(BuildContext context) {
  return new FutureBuilder(
    future: Firestore.instance.collection('games').document(this.documentId).get(),
    builder: (BuildContext gamesContext, AsyncSnapshot gamesSnapshot) {
      return new FutureBuilder(
        future: Firestore.instance.collection('profiles').document(gamesSnapshot.data['authorId']).get(),
        builder: (BuildContext profilesContext, AsyncSnapshot profilesSnapshot) {
          // Code goes here.
        }
      ),
    }
  ),
}

一定有更清洁的方法,对吧?

【问题讨论】:

  • 您可以使用FutureBuilderFuture.wait() 来等待多个期货完成。
  • 如果您想了解更多信息,请添加演示您尝试过的内容和尝试完成的内容的代码。
  • @GünterZöchbauer 添加了示例。

标签: firebase dart flutter


【解决方案1】:

你可以用一个FutureBuilder来做到这一点:

Future<AsyncSnapshot> getProfiles() async {
  var gamesSnapshot = await Firestore.instance.collection('games').document(this.documentId).get();
  return Firestore.instance.collection('profiles').document(gamesSnapshot.data['authorId']).get()
}

@override
Widget build(BuildContext context) {
  return new FutureBuilder(
    future: getProfiles(),
    builder: (BuildContext profilesContext, AsyncSnapshot profilesSnapshot) {
      // Code goes here.
    }
  ),
}

【讨论】:

  • 谢谢!请注意,Firebase .get() 返回的是 DocumentSnapshot,而不是 AsyncSnapshot。
猜你喜欢
  • 2013-06-17
  • 2010-09-26
  • 2021-06-25
  • 1970-01-01
  • 1970-01-01
  • 2022-08-17
  • 2017-05-06
  • 1970-01-01
  • 2015-09-02
相关资源
最近更新 更多