firebase - 为小部件调用两个不同端点的最佳实践?

标签 firebase dart flutter

当需要两个不同的端点来呈现某些内容时,最佳做法是什么?现在我正在嵌套 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.
        }
      ),
    }
  ),
}

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

最佳答案

您可以使用单个 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 - 为小部件调用两个不同端点的最佳实践?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50341710/

相关文章:

Flutter:元素类型 'String'无法分配给列表类型 'Widget'

dart - Flutter:测试期间的计时器问题

flutter - 状态错误 : Unexpected diagnostics: Expected an identifier. [严重] json_serializable :json_serializable on test/widget_test. dart

firebase - Firestore 安全规则 - 权限被拒绝

android - Firebase 检查用户的身份验证状态/如果用户存在

android - Firebase createUserWithEmailAndPassword 成功,但 oncomplete 从未运行

node.js - 使用 GeoFire 按半径查询位置

dart - Dart目录扫描和列表填充

testing - 在 VSCODE 上运行所有测试时出现 Flutter SIGINT 错误

flutter : How to increase indentation width of tab '\t' in text